Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Abrufen des übereinstimmenden Kontexts der MySQL-Volltextsuche in PHP (und Sicherheit)

Dies sollte Ihnen den Einstieg in den "Kontext"-Teil erleichtern...

// return the part of the content where the keyword was matched
function get_surrounding_text($keyword, $content, $padding)
{
    $position = strpos($content, $keyword);
    // starting at (where keyword was found - padding), retrieve
    // (padding + keyword length + padding) characters from the content
    $snippet = substr($content, $position - $padding, (strlen($keyword) + $padding * 2));
    return '...' . $snippet . '...';
}

$content = 'this is a really long string of characters with a magic word buried somewhere in it';
$keyword = 'magic';
echo get_surrounding_text($keyword, $content, 15); // echoes '... string with a magic word in it...'

Diese Funktion berücksichtigt keine Fälle, in denen die Füllgrenzen außerhalb der Inhaltszeichenfolge liegen würden, z. B. wenn das Schlüsselwort am Anfang oder Ende des Inhalts gefunden wird. Es berücksichtigt auch nicht mehrere Übereinstimmungen usw. Aber es sollte Sie hoffentlich zumindest in die richtige Richtung weisen.