note 90472 deleted from function.str-ireplace by danbrown

From: Date: Mon, 18 May 2009 14:46:26 +0000
Subject: note 90472 deleted from function.str-ireplace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155677@lists.php.net to get a copy of this message
Note Submitter: james dot proud at gmail dot com ---- After trying each of the highlight scripts, but each was missing a part I required, so I made this. This one will search for a pattern, and return it in bold while retaining the capitalisation from the original string. <?php function highlight($needle, $haystack) { $first = str_ireplace($needle, '*', $haystack, $count); $second = $haystack; foreach (explode('*', $first) as $value) { $second = str_ireplace($value, '#', $second); } foreach (explode('#', $second) as $value) { if (!empty($value)) { $result = str_replace('*', '<strong>' . $value . '</strong>', $first); } } if ($count === 0) { return $haystack; } else { return $result; } } // Example usage echo highlight('amEs mOrR', 'James Morrison'); // J<strong>ames Morr</strong>ison ?>

« previous php.notes (#155677) next »