- Code: Select all
<?php
/**
* Join string by a regular expression.
*
* @author Joshua Rennert <admin@jphantom.com>
* @version 1.0.1
* @param string $glue Regular expression
* @param string $text Original text
* @param array $array Array pieces
* @return string
*/
function preg_join ( $glue, $text, $array )
{
preg_match_all ( addslashes ( $glue ), $text, $paste );
for ( $i = 0; $i < count ( $array ); $i++ )
{
$copy .= $array[$i] . $paste[0][$i];
}
$join = $copy;
return $copy;
}
?>
So say you want to make the word 'upload' bold, but don't want it to interefere with a URL that has the word 'upload' in it (ex: http://yoursite.com/upload.php).
You would first split your text:
- Code: Select all
$cut = preg_split ( '/<a href="(.*?)">/', $your_text );
Next, you would run a loop and bold the word 'upload':
- Code: Select all
for ( $i = 0; $i < count ( $cut ); $i++ )
{
$cut[$i] = preg_replace ( '/upload/i', '<b>upload</b>', $cut[$i] );
}
Finally, since there is no real function in PHP that can implode an array with regex, you can utilize the small function I created above:
- Code: Select all
$highlighted_text = preg_join ( '/upload/i', $your_text, $cut );
Hope this helps you in some fashion. Happy coding.
// Phantom

