john mckean Posted March 5, 2011 Share Posted March 5, 2011 I am translating my javascript function to php and am having trouble referencing captured text. Specifically, RegExp.$1 returns the captured text, but ${1} returns empty. I have spent a great deal of time researching this, to no avail. Thanks for any help. John This loop steps though the text looking for potential linkable URLs or email addresses. Each match is replaced with a mark <1> .. <5> etc. A subsequent loop analyzes each capture and replaces the mark with HTML link code or restores the original text. The javascript: for (i=0;i<20;i++) { // Get a potential link and mark where it came from $text = $text.replace(/(\S+\.\S+)/,"<"+i+">"); // look for dots that are surrounded by non-whitespace characters tlnk[i] = RegExp.$1; } My attempted PHP translation: // Loop through the clear text for ($i=0;$i<20;$i++) { // Get a potential link and mark where it came from $text = preg_replace('(\S+\.\S+)',"<".$i.">",$text,1); // look for dots that are surrounded by non-whitespace characters $tlnk[$i] = ${1}; } Quote Link to comment https://forums.phpfreaks.com/topic/229665-preg_replace-referencing-the-captured-text/ Share on other sites More sharing options...
sasa Posted March 5, 2011 Share Posted March 5, 2011 try preg_match('/(\S+\.\S+)/', $text, $out); print_r($out); Quote Link to comment https://forums.phpfreaks.com/topic/229665-preg_replace-referencing-the-captured-text/#findComment-1183234 Share on other sites More sharing options...
john mckean Posted March 6, 2011 Author Share Posted March 6, 2011 Thanks for the reply sasa, but preg_match doesn't do the replace. Is there some way of seeing capture values on a preg_replace as there is in javascript? OR ... Can you guide me to a foolproof function that makes links from text URLs? Thanks, John Quote Link to comment https://forums.phpfreaks.com/topic/229665-preg_replace-referencing-the-captured-text/#findComment-1183480 Share on other sites More sharing options...
sasa Posted March 6, 2011 Share Posted March 6, 2011 try[codeg<?php function my_link($a = 'i_need_link'){ static $i = 0, $array = array(); if($a == 'i_need_link') return $array; $i++; $array[$i]= $a[0]; return "<$i>"; } $test = 'blah blah www.a.com bla bla blaa b.com aaaa sss c.org bla'; $test1 = preg_replace_callback('/\S+\.\S+/', 'my_link', $test); $links = my_link(); echo $test1,"\n<hr />\n"; print_r($links); ?> Quote Link to comment https://forums.phpfreaks.com/topic/229665-preg_replace-referencing-the-captured-text/#findComment-1183510 Share on other sites More sharing options...
john mckean Posted March 7, 2011 Author Share Posted March 7, 2011 Thanks sasa, it works great! That's probably the best example of preg_replace_callback I've seen. I can use it elsewhere too. Much obliged, John Quote Link to comment https://forums.phpfreaks.com/topic/229665-preg_replace-referencing-the-captured-text/#findComment-1183991 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.