Ví dụ, nó chỉ trả về đoạn mã mà từ khóa tìm kiếm tồn tại.Cách tạo đoạn mã như đoạn mã được tạo bởi Google bằng PHP và MySQL? Ví dụ:
Và một phần văn bản được thay thế bằng "...".
Có thể đạt được mục tiêu đó với PHP và MySQL không?
Ví dụ, nó chỉ trả về đoạn mã mà từ khóa tìm kiếm tồn tại.Cách tạo đoạn mã như đoạn mã được tạo bởi Google bằng PHP và MySQL? Ví dụ:
Và một phần văn bản được thay thế bằng "...".
Có thể đạt được mục tiêu đó với PHP và MySQL không?
$snippet = "//mysql query";
function trimmer($updates,$wrds){
if(strlen($updates)<=$wrds){
return $updates;
}else{
$marker = strrpos(substr($updates,0,$wrds),' ');
$string = substr(substr($updates,0,$wrds),0,$marker)."...";return $string;
}
echo trimmer($snippet,200); //You can send the snippet string to this function it searches for the last space if string length is greater than 200 and adds "..." to it
Đây có lẽ là những gì bạn muốn (EDIT):
$string1="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
function trimmer($updates,$wrds,$pos){
if(strlen($updates)<=$wrds) {
return $updates;
} else {
$marker = strrpos(substr($updates,$pos,$wrds),' ');
$string = substr(substr($updates,$pos,$wrds),0,$marker)."...";
return $string;
}
}
$pos = strpos($string1, "dummy");
echo trimmer($string1,100,$pos);
Hãy nghĩ xem nếu từ khóa tồn tại 201 ký tự từ đầu. Bạn đã trả về 200 ký tự đầu tiên, nhưng không bao gồm từ khóa. – omg
Tôi đã thêm một EDIT ... có thể đây là những gì bạn muốn – halocursed
function excerpt($text, $phrase, $radius = 100, $ending = "...") {
$phraseLen = strlen($phrase);
if ($radius < $phraseLen) {
$radius = $phraseLen;
}
$pos = strpos(strtolower($text), strtolower($phrase));
$startPos = 0;
if ($pos > $radius) {
$startPos = $pos - $radius;
}
$textLen = strlen($text);
$endPos = $pos + $phraseLen + $radius;
if ($endPos >= $textLen) {
$endPos = $textLen;
}
$excerpt = substr($text, $startPos, $endPos - $startPos);
if ($startPos != 0) {
$excerpt = substr_replace($excerpt, $ending, 0, $phraseLen);
}
if ($endPos != $textLen) {
$excerpt = substr_replace($excerpt, $ending, -$phraseLen);
}
return $excerpt;
}
shamelessly bị đánh cắp từ Cake TextHelper.
Chức năng lừa dối được sửa đổi một chút để cho phép nhiều cụm từ. ví dụ. cụm từ của bạn có thể là "testa testb" và nếu nó không tìm thấy testa, thì nó sẽ đi đến testb.
function excerpt($text, $phrase, $radius = 100, $ending = "...") {
$phraseLen = strlen($phrase);
if ($radius < $phraseLen) {
$radius = $phraseLen;
}
$phrases = explode (' ',$phrase);
foreach ($phrases as $phrase) {
$pos = strpos(strtolower($text), strtolower($phrase));
if ($pos > -1) break;
}
$startPos = 0;
if ($pos > $radius) {
$startPos = $pos - $radius;
}
$textLen = strlen($text);
$endPos = $pos + $phraseLen + $radius;
if ($endPos >= $textLen) {
$endPos = $textLen;
}
$excerpt = substr($text, $startPos, $endPos - $startPos);
if ($startPos != 0) {
$excerpt = substr_replace($excerpt, $ending, 0, $phraseLen);
}
if ($endPos != $textLen) {
$excerpt = substr_replace($excerpt, $ending, -$phraseLen);
}
return $excerpt;
}
chức năng Highlight
function highlight($c,$q){
$q=explode(' ',str_replace(array('','\\','+','*','?','[','^',']','$','(',')','{','}','=','!','<','>','|',':','#','-','_'),'',$q));
for($i=0;$i<sizeOf($q);$i++)
$c=preg_replace("/($q[$i])(?![^<]*>)/i","<span class=\"highlight\">\${1}</span>",$c);
return $c;}
Giải pháp của tôi cho nhiều nhiều từ khóa và nhiều lần xuất hiện (cũng làm việc cho điểm nhấn case insensitive):
function excerpt($text, $query)
{
//words
$words = join('|', explode(' ', preg_quote($query)));
//lookahead/behind assertions ensures cut between words
$s = '\s\x00-/:[email protected]\[-`{-~'; //character set for start/end of words
preg_match_all('#(?<=['.$s.']).{1,30}(('.$words.').{1,30})+(?=['.$s.'])#uis', $text, $matches, PREG_SET_ORDER);
//delimiter between occurences
$results = array();
foreach($matches as $line) {
$results[] = htmlspecialchars($line[0], 0, 'UTF-8');
}
$result = join(' <b>(...)</b> ', $results);
//highlight
$result = preg_replace('#'.$words.'#iu', "<span class=\"highlight\">\$0</span>", $result);
return $result;
}
Đây là ví dụ kết quả cho truy vấn = "Švihov prohlídkám "
fyi- điều này dường như không khớp nếu dòng trong văn bản $ chứa truy vấn $ không có dòng mới trước và sau dòng. $ text = "\ n $ {text} \ n" là một giải pháp đơn giản, nhưng regex có thể cần điều chỉnh – dlo
Điều này trả về các trích đoạn rất dài nếu bạn có văn bản dài và nhiều lần truy cập. – jor
Công cụ tuyệt vời! Cảm ơn. –
chúng được gọi là dấu chấm lửng (en) en.wikipedia.org/wiki/Ellipsis – Strawberry