note 90472 added to function.str-ireplace
| From: | james dot proud at gmail dot com | Date: | Fri, 24 Apr 2009 00:59:19 +0000 |
| Subject: | note 90472 added to function.str-ireplace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-153642@lists.php.net to get a copy of this message | ||
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
?>
----
Server IP: 87.124.35.190
Probable Submitter: 86.7.39.121
----
Manual Page -- http://www.php.net/manual/en/function.str-ireplace.php
Edit -- https://master.php.net/note/edit/90472
Del: integrated -- https://master.php.net/note/delete/90472/integrated
Del: useless -- https://master.php.net/note/delete/90472/useless
Del: bad code -- https://master.php.net/note/delete/90472/bad+code
Del: spam -- https://master.php.net/note/delete/90472/spam
Del: non-english -- https://master.php.net/note/delete/90472/non-english
Del: in docs -- https://master.php.net/note/delete/90472/in+docs
Del: other reasons-- https://master.php.net/note/delete/90472
Reject -- https://master.php.net/note/reject/90472
Search -- https://master.php.net/manage/user-notes.php