0

trying to fix this regex. Its supposed to find any hyperlinks in a string and put anchor tags around them. Keeps coming back, unkown identifier '+'. I thought the plus sing was part of regex?

<?php

//replace links with clickable links

// match protocol://address/path/
$comments = preg_replace("[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\">\\0</a>", $comments);

// match www.something
$comments = preg_replace("(^| )(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"http://\\2\">\\2</a>", $comments);

?>

any help appreciated.

SlickRick
  • 25
  • 3
  • 8

2 Answers2

7

A PCRE patterns (that's what you give to preg_replace) needs to be enclosed by delimiters:

~[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*~

Here the ~ are the delimiters. I used this char because it doesn't occur in the rest of the regex.

To explain the error: PCRE thinks that [ is the delimiter (as the first char always is the delimiter). So when it find the corresponding closing delimiter ] is considers everything after it a modifier. And as there is no + modifier you get an error ;)

NikiC
  • 100,734
  • 37
  • 191
  • 225
  • Thank you sir. an I thought regular regex was hard enough, now I have to grok PCRE, lol – SlickRick Jun 17 '11 at 10:53
  • If you actually use the ~, you can escape it with \~ in the regex. If the regex is built dynamically, you can use preg_quote. The delimiter is what you want, I generally use /, ~ or #, depending on the regular expression. – Berry Langerak Jun 17 '11 at 10:57
  • do I put the tildes around the 'replace' part too? (the 2nd arg?) – SlickRick Jun 17 '11 at 11:02
  • im not getting any errors now, but the anchor tags aren't being added – SlickRick Jun 17 '11 at 11:07
-1

try replacing

"[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\">\\0</a>"

with

r'[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*', '<a href=\"\\0\">\\0</a>'
pharno
  • 46
  • 2
  • 7