php - RegEx troubles with converting text to links -
i'm trying make shoutbox automatically convert posted data links if matches criteria. works, except not if there more 1 link in single shoutbox post.
here code meant convert it:
<? $text = "edit:4961310112967 - edit:021331612649 these correct?"; echo $text."<br><br>"; $view = "/(view|edit)\:[0-9]+(\s)?/"; if(preg_match_all($view, $text, $url, preg_set_order)) { foreach ($url $val) { echo "(matched: " . $val[0] . ")\n"; $checkcode = explode(":", $val[0]); if(strcmp($checkcode[0], 'view') == 0) { $text = preg_replace($view, '<a target="_blank" style="text-decoration: underline;color:purple;font-weight:bold;" href="../index.php?upc='.$checkcode[1].'">(view product)</a>', $text); } else if(strcmp($checkcode[0], 'edit') == 0) { $text = preg_replace($view, '<a target="_blank" style="text-decoration: underline;color:purple;font-weight:bold;" href="../edit.php?upc='.$checkcode[1].'">(edit product)</a>', $text); } echo "<br>".$text; echo "<br>(".$checkcode[0]." - ".$checkcode[1].")<br><br>"; } } echo "<br><br>".$text."<br><br>"; ?>
as can see, $text variable @ top has 2 spots says "edit:" - meant call regex turn link. link in question be, example,
<a href="edit.php?upc=############"</a>
currently, how is, when there more 1 call regex, makes both links first edit:
sorry, don't feel explaining well.
this string: edit:4961310112967 - edit:021331612649 these correct? regex make both links edit.php?upc=4961310112967
i simplify bit dividing in 2 regular expressions.
https://regex101.com/r/bn9qy5/2
$re = "/edit\\:([0-9]+(\\s)?)/m"; $str = "edit:4961310112967 - edit:021331612649 these correct?\n\nedit:1231289389123\n\nedit:123123lkajsdad\n\nview:234234234234234"; $subst = "<a href=\"edit.php?upc=$1\"</a>"; $result = preg_replace($re, $subst, $str);
and same view. hope helps!
Comments
Post a Comment