Find All Matched Strings and Highlight Them

Written by @m_k_amin 29 December 2012

You know that the PHP finding functions (str_pos, strrpos and etc) just finds one matched string. This short script shows how to find all matched strings. It also highlights the found items.

Code Snippet:

                                                
                                                <!-- this script is provided by www.phpfreecode.com coded by: Kerixa Inc. -->
<!-- This Script is from www.phpfreecpde.com, Coded by: Kerixa Inc-->

<?php
$txt='1mk23mk4mk56mmk67mkk';
$find='mk';

echo 'The search expression is found at <br>';
$a=-1;
while(is_numeric($a)){
	$a++;
	$a= strpos($txt,$find,$a);
	if (is_numeric($a)) echo $a.'<br>';
}
echo 'characters.<br><br>';
$replace="<span style='background-color: #EEEE88'>$find</span>";
echo str_replace($find,$replace,$txt);
?>

<div style="text-align: left">
	<br>
<font face="Tahoma">
<span style="font-size: 8pt; text-decoration: none"><a target="_blank" href="http://www.phpfreecode.com/">
	PHP Free Code</a></span></font>
</div>
<a target='_blank' href='www.phpfreecode.com' style='font-size: 8pt; text-decoration: none'>Php Best Codes</a>                                                
                                            

Example:


About @m_k_amin

This user is pending a biography.

M

Comments


Here you can leave us commments. Let us know what you think about this code tutorial!

0 / 300

TRENDING POST
1
2
3
4
5
VISITORS
Online Users: 12
Recent Members: grkkid, Manaakividuinfo.com, karticksv, sava, tinatina
advertisement 2