0

Possible Duplicate:
Troubleshooting “preg_replace(): Unknown modifier”

My pattern for detecting the URL is: \b[(http://|https://)]*[www]*[\.]*(\S+\.[a-z]{2,})/* while my replacement pattern is \1\2. It's not seemingly working, however, as I get this error:



Warning: preg_replace(): Unknown modifier '/' in [...][...] on line 1


What am I doing wrong?

Also, if someone could mention how to detect the fact a URL could have multiple periods in it for subdomains (like name.subdomain.canada.gov) how would I detect that infinitely? I know I could have \S*\.* but isn't that like "there could be another set of subdomains and another period, but not necessarily" what if there's another sequence of five more subdomains?

My actual usage would be: preg_replace("\b[(http://|https://)]*[www]*[\.]*(\S+\.[a-z]{2,})/*", "\1\2", $text);

Community
  • 1
  • 1
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
  • Wrong delimiter, or no delimiter. You'll have to figure this out yourself, because you didn't show your actual code/usage. – mario Feb 05 '13 at 00:45
  • More specifically: [converting ereg to preg (missing regex delimiters)](http://stackoverflow.com/questions/6270004/converting-ereg-expressions-to-preg) – mario Feb 05 '13 at 00:49

1 Answers1

1

PHP Needs an surrounding char to seperate regex modifiers and the regex i.e. preg_replace("~yourregex~",...)

But there are more errors in your Expression which should be fixed also. You use the [] and () wrong. A better Expression for a simple ulr matcher could be - for your Special scenario this must be changed to your needs:

"~(?:http://|https://)(?:[a-z0-9_-]+\.)+(?:[a-z0-9_-]+)~"
Philipp
  • 15,377
  • 4
  • 35
  • 52
  • If I used the surrounding char and then changed it to `\b([http://|https://])*[www]*[\.]*(\S+\.[a-z]{2,})/*` would it work? I was under the impression you use [ ] for specifying almost a group of specific matches, for lack of a better term, such as [a|b] and ( ) for back references. – Doug Smith Feb 05 '13 at 01:01
  • [] is the Syntax for char-classes, which match only one possbile char - what you want is (?:http://|https://). The ?: at the beginning of the parenthesis tell the regex matcher to threat this as a subpattern – Philipp Feb 05 '13 at 01:08
  • Have a look at http://www.php.net/manual/en/pcre.pattern.php – Philipp Feb 05 '13 at 01:10