i'm using a regular expression to search for a bunch of keywords in a text.
All keywords are found but one: [DAM]Berlin. I know it contains a square bracket so i escaped it, but still, no luck. What am i doing wrong?
here is my php code.
The text to search for keywords:
$textToSearch= '<p><br>
Time ¦ emit LAb[au] <br>
<br>
[DAM]Berlin gallery<br>
<br>
Exhibition: February 21st - March 28th, 2009 <br>
<br>
Opening: Friday, February 20th, 2009 7-9 pm <br>';
The regular expression:
$find='/(?![^<]+>)\b(generative art console|Game of Life|framework notations|framework|Floating numbers|factorial|f5x5x3|f5x5x1|eversion|A-plus|16n|\[DAM\]Berlin gallery)\b/s';
the replace Callback function:
function replaceCallback( $match )
{
if ( is_array( $match ) )
{
$htmlVersion = htmlspecialchars( $match[1], ENT_COMPAT, 'UTF-8' );
$urlVersion = urlencode( $match[1] );
return '<a class="tag" rel="tag-definition" title="Click to know more about ' . $htmlVersion . '" href="?tag=' . $urlVersion. '">'. $htmlVersion . '</a>';
}
return $match;
}
and finally, the call:
$tagged_content = preg_replace_callback($find, 'replaceCallback', $textToSearch);
Thank you for your help !
I think it's because
[
isn't a "word character", so\b[
can't match[
in the beginning of[DAM]Berlin
. You probably need to change your regex to:Edit: From Daniel James's comment:
The first section of your Regex is '/(?![^<]+>)\b' so wouldn't it only match "[DAM]Berlin gallery" if the character before it was a '>'?
try:
That adds the m modifier to your regex so that it will ignore new lines