0

I have the following regex which works fine in a regex editor but when I pull it together in PHP i am getting and Unknown modifier '(' error come up.

preg_replace("(\[LINK\])(\S*)(\[\/LINK])", "<a>href=\'$2\'>$2</a>", $xtext);

This is my first question on SO so I hope I have given enough information. From my research I believe I am missing delimiters but tried ~ at the start and the end of the search pattern and still does not seem to work.

MikeC
  • 91
  • 6
  • 1
    [Possible duplicate of all these questions...](http://stackoverflow.com/search?q=%5Bphp%5D+%5Bregex%5D+Unknown+modifier+%27%28%27+is%3Aquestion) – HamZa May 23 '14 at 10:19

2 Answers2

0

try this

preg_replace("/(\[LINK\])(\S*)(\[\/LINK])/", "<a>href=\'$2\'>$2</a>", $xtext);

Note the delimiters

Chris Lear
  • 6,592
  • 1
  • 18
  • 26
0

Just try with:

$input  = 'foo [LINK]http://google.com[/LINK] bar';
$output = preg_replace('/\[LINK\](.*?)\[\/LINK\]/', '<a href="$1">$1</a>', $input);

Output:

string 'foo <a href="http://google.com">http://google.com</a> bar' (length=57)
hsz
  • 148,279
  • 62
  • 259
  • 315