Warning: unlink(./data/secure/tokens/078fa34b912e2a58e35af22c4a90fb737bebf072c06ef750cfb63907a2484a5b455a720e8dfce88c8b8f78fb8470194cd48cbe957f84c52a1720af64738d7b6c.token): No such file or directory in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 865

Warning: unlink(./data/secure/tokens/1e4a91a486f8adc8fff8adb00279bf0d9b48ad8714443e261ad99b05d5fd653d82aa550e4a182d508f8ce541a94b91d0ae5ca93967ad228068792737c6d77c64.token): No such file or directory in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 865

Warning: unlink(./data/secure/tokens/32b558f9ec34324fa6fe4e67dc39dec3ae871342b2b76d2ed831c1ec1f355cf2642139fdbf540b8fa640b0ab5e9056a7b4e91b2ce6319e61aaae8f571d052a07.token): No such file or directory in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 865

Warning: unlink(./data/secure/tokens/3a1a40d959d9b9b62ac7edee3a114fee42a2faf708087e6fcacc256251a0681122d75a091d42b0bdc271d48d0b99507d3032cf484111077f1af2c28a8f5d4d50.token): No such file or directory in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 865

Warning: filemtime(): stat failed for ./data/secure/tokens/3b48352db76cc58a698c643b5aa9cc3e0efeee842db5e54941a9bb232498816c0b9cda084062d252c0530f5ec6e7d07d030c50d5415159270302936b772281a7.token in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 831

Warning: filemtime(): stat failed for ./data/secure/tokens/42cd6880eb312563764430b33a641c74a4d10c2450a100cae79c7c7a90336f7ed8746d53b10d4c7780fb51e66c92f75e20007c70420c9a80547e551545054ac9.token in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 831

Warning: filemtime(): stat failed for ./data/secure/tokens/52ab04778770131da3ca3ca0b6fa0d811d300c59dd183a1f6429dfc69f3b9a1789844ea0a05ec0bc674d0abd17d63ddba03e5af486708b11d5877f1b3ca38772.token in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 831

Warning: filemtime(): stat failed for ./data/secure/tokens/5905477552c2bd9f27cbef85be6ab3d591bd886a453f8f80f4efa73c45d728d69986420a5a60227b421806f309f9e82f43fe84e8fae67a48f6b1ed0776734171.token in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 831

Warning: unlink(./data/secure/tokens/667f4653065ca9bd7fe5cdcd98b86fcaa59bba8b4dc7fd470ae6943ae9a408fb533184aac7358a2fdfb9f8d7ce0dfac467596f611f5f6460a3236ea2aae34237.token): No such file or directory in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 865

Warning: unlink(./data/secure/tokens/6b52861f4fa4630ea400c78249b3c6ef98ae3e10b8f27e91bd2aa5feb849141ea945b909ac12a0b9a7fc52030a0c126b4ed8e0c6caee54e3fc0647f021d785d8.token): No such file or directory in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 865

Warning: filemtime(): stat failed for ./data/secure/tokens/6bb1a68366a752f237a8b06929df6f45b4e63d76dfb136811f171de015008e6b8b4077d2146003e78d3e46e492676f8535541983593399c44d18c37c519a8cba.token in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 831

Warning: filemtime(): stat failed for ./data/secure/tokens/87bdf1cbdf0a12344c7e854c6464c376067d490e9fa168d8615883987b3c4c05b0860a1a01acfff3f82b27a78421830de0786faa8176bebf58e7983653b88b3f.token in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 831

Warning: unlink(./data/secure/tokens/973e81b830d4bf120bfe54e1c20dd8fe78b3c4f9e538a79a1df48f410a968a0117dfbc424babd7c0e154c38ce7ef461296cb09b54baba86a21deee1f062e625f.token): No such file or directory in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 865

Warning: filemtime(): stat failed for ./data/secure/tokens/a7eb80ad9ad1efa30252e5da152a599f2c248dfad8747c48d2c501ef094f978d498c519bb99af64ebe114671ecf65bad79b1c4ed9cde4005f1084e66250f15f2.token in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 831

Warning: unlink(./data/secure/tokens/aa921cf3a8d382884cff06656de608b766f4919358bb51f5fdc607b5d4b3eecba790bc7a82bc0ed914b63b13efbbcf1aeb7888eadc5a6d232437fd3ac2d5bba9.token): No such file or directory in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 865

Warning: filemtime(): stat failed for ./data/secure/tokens/c2677fd88f79d29cc1626f7687a33c5e3bec2535523b2dfe2bd6c4920b4e147403147be9f6e312ac4fc2e15ffb67c33684e75859749be0e6152cc15664ddf43f.token in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 831

Warning: unlink(./data/secure/tokens/cce0988e6c5dae49d1662e3d9b8ed09826eccf77c947f8c442fc1a9675acb69ec2b5899830fe76a772b386a8a3880bad40e95c4086e45e39be8f3f182f4336b2.token): No such file or directory in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 865

Warning: filemtime(): stat failed for ./data/secure/tokens/d7cf1c18f4e8e511dee3f0adc150f457094b9b8e3ea1981a6de44ab0d512f42c272fb8a895109e296e8b2cfbd447bc7911dfc1ceae43334f5155c2c3e242fd5f.token in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 831

Warning: unlink(./data/secure/tokens/e0c1ea97b52107b0f31058c878447dc270e4ba0854217ebdd0e9a0f9507ea8d82e64e9f3a4e9441d8f6a85b8d7ece839b6b3ef70ad9e4bf340b22ce971cfde0a.token): No such file or directory in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 865

Warning: unlink(./data/secure/tokens/e8f1354c883143a7b00c17a643f9f278865bf63b528877e1fc34bd384572169d2241c1d8c8b6e0a036596117a742463d04651fa9203a38e1f3aaa6d1d7a19186.token): No such file or directory in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 865
SnippetVamp - open
#key - Aidan Lister » Blog Archive » Highlighting a search string in HTML text
/**
 * Perform a simple text replace
 * This should be used when the string does not contain HTML
 * (off by default)
 */
define('STR_HIGHLIGHT_SIMPLE', 1);
 
/**
 * Only match whole words in the string
 * (off by default)
 */
define('STR_HIGHLIGHT_WHOLEWD', 2);
 
/**
 * Case sensitive matching
 * (off by default)
 */
define('STR_HIGHLIGHT_CASESENS', 4);
 
/**
 * Overwrite links if matched
 * This should be used when the replacement string is a link
 * (off by default)
 */
define('STR_HIGHLIGHT_STRIPLINKS', 8);
 
/**
 * Highlight a string in text without corrupting HTML tags
 *
 * @author      Aidan Lister 
 * @version     3.1.1
 * @link        http://aidanlister.com/2004/04/highlighting-a-search-string-in-html-text/
 * @param       string          $text           Haystack - The text to search
 * @param       array|string    $needle         Needle - The string to highlight
 * @param       bool            $options        Bitwise set of options
 * @param       array           $highlight      Replacement string
 * @return      Text with needle highlighted
 */
function str_highlight($text, $needle, $options = null, $highlight = null)
{
    // Default highlighting
    if ($highlight === null) {
        $highlight = '1';
    }
 
    // Select pattern to use
    if ($options & STR_HIGHLIGHT_SIMPLE) {
        $pattern = '#(%s)#';
        $sl_pattern = '#(%s)#';
    } else {
        $pattern = '#(?!<.*?)(%s)(?![^<>]*?>)#';
        $sl_pattern = '#(%s)#';
    }
 
    // Case sensitivity
    if (!($options & STR_HIGHLIGHT_CASESENS)) {
        $pattern .= 'i';
        $sl_pattern .= 'i';
    }
 
    $needle = (array) $needle;
    foreach ($needle as $needle_s) {
        $needle_s = preg_quote($needle_s);
 
        // Escape needle with optional whole word check
        if ($options & STR_HIGHLIGHT_WHOLEWD) {
            $needle_s = 'b' . $needle_s . 'b';
        }
 
        // Strip links
        if ($options & STR_HIGHLIGHT_STRIPLINKS) {
            $sl_regex = sprintf($sl_pattern, $needle_s);
            $text = preg_replace($sl_regex, '1', $text);
        }
 
        $regex = sprintf($pattern, $needle_s);
        $text = preg_replace($regex, $highlight, $text);
    }
 
    return $text;
}

// Simple Example
$string = 'This is a site about PHP and SQL';
$search = array('php', 'sql');
echo str_highlight($string, $search);
echo "n";
  
// With HTML in the text
$string = 'Link to php';
$search = 'php';
echo htmlspecialchars(str_highlight($string, $search));
echo "n";
  
// Matching whole words only
$string = 'I like to eat bananas with my nana!';
$search = 'Nana';
echo str_highlight($string, $search, STR_HIGHLIGHT_SIMPLE|STR_HIGHLIGHT_WHOLEWD);
echo "n";
  
// With custom highlighting
$string = 'With custom highlighting!';
$search = 'custom';
$highlight = '1';
echo str_highlight($string, $search, STR_HIGHLIGHT_SIMPLE, $highlight);
echo "n";
  
// With links
$string = 'I am a link';
$search = 'link';
$highlight = '1';
echo htmlspecialchars(str_highlight($string, $search, STR_HIGHLIGHT_STRIPLINKS, $highlight));

This is a site about PHP and SQL
Link to php
I like to eat bananas with my nana!
With custom highlighting!
I am a link