Regex syntax to deal with carriage returns in php -
i've been working on coming regex syntax deal making string patterns clickable links form. carriage returns causing problems regex pattern , need understanding how omit them. example, if enter text text area;
http://www.google.com http://www.google.com www.google.com google.com
this output before regex pattern sees it;
http://www.google.com\r\nhttp://www.google.com\r\nwww.google.com\r\ngoogle.com
i need able remove \r\n characters hyperlinks. regex looks this;
function make_links_clickable($message) { return preg_replace('!(((.*www\.)?(f|ht)tp(s)?://)?[-a-za-zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', '<a href="http://$0" target="_blank">$0</a>', $message); }
can tell me how remove leading \r\n characters in regex?
an easy way split on new lines, , run regex on each piece. observe:
function make_links_clickable($message) { $result = array(); foreach(explode(php_eol,$message) $m) { $result[] = preg_replace('!(((.*www\.)?(f|ht)tp(s)?://)?[-a-za-zа-яА-Я()0-9@:%_+.~#?&;//=]+)!i', '<a href="http://$0" target="_blank">$0</a>', $m); } return join(php_eol,$result); }
alternatively use preg_filter
Comments
Post a Comment