php - Regex - replace all the 11 digit numbers with link -
i trying replace 11 digit numbers links.
right preg_match_all not returning matches strings, returning last matched one. preg_replace returning single 11 digit number link.
//string numbers replaced links. $test ="test these numbers 20150423011 20150423012 sdf sdfsdf sdfs fsdf sdfs dfsdfsd 20150423014 fsdf"; $s = preg_match_all("/^((?:.*?)(20\d{9})(?:.*?))+$/",$test,$matches); foreach($matches[2] $m){ $replacements[] = "<a href=''>".$m."</a>"; $patterns[] = "/^((?:.*?)(20\d{9})(?:.*?))+$/"; } $final = preg_replace($patterns, $replacements,$test); echo $final; //right gives out last matched number link
i'm not sure if understood question, assume want wrap numbers starting 20 using <a href....
for case, can use regex:
(20\d{9}) you can use code:
$re = "/(20\\d{9})/"; $str = "test these numbers 20150423011 20150423012 sdf sdfsdf sdfs fsdf sdfs dfsdfsd 20150423014 fsdf"; $subst = "<a href=''>$1</a>"; $result = preg_replace($re, $subst, $str); your resulting string be:
test these numbers <a href=''>20150423011</a> <a href=''>20150423012</a> sdf sdfsdf sdfs fsdf sdfs dfsdfsd <a href=''>20150423014</a> fsdf
Comments
Post a Comment