Code: Select all
preg_match_all('(((f|ht){1}(tp://|tps://))[-a-zA-Z0-9@:%_+.~#?&//=]+)', $text, $hyperlinksArray);Code: Select all
preg_match_all('\b(https|http)://([a-zA-Z-:@.0-9]+)(/((\([-;:@&=a-zA-Z0-9$_.+!*\',]*?\))|[-;:@&=?a-zA-Z0-9$_.+!*\',]|%\d\d)+)?(?<![,.;])', $text, $hyperlinksArray);My problem is every time this code is parsed it returns “Warning: preg_match_all() [function.preg-match-all]: Delimiter must not be alphanumeric or backslash in [file] on [line]” and no replacements are made. Is this an issue with the regex itself, escaping, or something with PHP? I am using PHP 5. An example of $text would be "Check out this cool Wikipedia page: http://en.wikipedia.org/wiki/Textile_(disambiguation)" and the last string is defined as `$hyperlinksArray = array();`.
The problem with the original regex is that it does not work with parentheses and it includes periods, commas, semicolons, and close parenthesis at the end of a URL in the URL itself, which, in all cases except semicolons, is NOT what Twitter does. Since I am shortening URLs with Bit.ly, using the original code on my example will produce a Bit.ly short URL for the long URL http://en.wikipedia.org/wiki/Textile_ instead of http://en.wikipedia.org/wiki/Textile_(disambiguation) as it should. Philippe's code would solve these problems and several others if I could get it to work.