Search code examples
phpregexsquare-bracket

how to find a string containing a square bracket?


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 !


Solution

  • 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:

    $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';
    

    Edit: From Daniel James's comment:

    This might be closer to the original intent, as it will still check that '[Dam]' doesn't follow a word character:

    $find='/(?![^<]+>)(?<!\w)(generative art console|Game of Life|framework notations|framework|Floating numbers|factorial|f5x5x3|f5x5x1|eversion|A-plus|16n|\[DAM\]Berlin gallery)\b/s';