StirCrazy Posted June 30, 2007 Share Posted June 30, 2007 Hi folks, I'm trying to sensor some text variables (but in a specific way). Looks like this:- $text = "text text text BOB1 text text text"; $replacetext = "*****"; $var1 = array("BOB","JACK","BILLY","HARVEY","JOHN"); $var2 = array("1","2","3","4","5"); If $text has "BOB" anywhere in the variable then check if it has a number from $var2 joined to it. If it has the replace "BOB1" with $replacetext. Any ideas how to do this? Will love you forever if you can help. S.C> Link to comment https://forums.phpfreaks.com/topic/57872-solved-text-replace-from-array/ Share on other sites More sharing options...
utexas_pjm Posted June 30, 2007 Share Posted June 30, 2007 <?php $text = "text BILLY3 text BOB1 text text JOHN1"; $replacetext = "*****"; $var1 = array("BOB","JACK","BILLY","HARVEY","JOHN"); $var2 = array("1","2","3","4","5"); // Couldn't you just use tihs array? // $compArray = array("BOB1","JACK2","BILLY3","HARVEY4","JOHN5") // if not, I'll build it. $count = count($var1); for($i = 0; $i < $count; ++$i){ $compArray[] = $var1[$i] . $var2[$i]; } foreach($var1 as $token){ $text = str_replace($compArray, $replacetext, $text); } // Is this what you're looking for? echo $text; ?> EDIT I see you want any numbert in var 2... try this one: <?php $text = "text BILLY3 text BOB1 text text JOHN1"; $replacetext = "*****"; $var1 = array("BOB","JACK","BILLY","HARVEY","JOHN"); $var2 = array("1","2","3","4","5"); $count = count($var1); for($i = 0; $i < $count; ++$i){ $count2 = count($var2); for($c = 0; $c < $count2; ++$c){ $compArray[] = $var1[$i] . $var2[$c]; } } foreach($var1 as $token){ $text = str_replace($compArray, $replacetext, $text); } echo $text; ?> Best, Patrick Link to comment https://forums.phpfreaks.com/topic/57872-solved-text-replace-from-array/#findComment-286777 Share on other sites More sharing options...
StirCrazy Posted June 30, 2007 Author Share Posted June 30, 2007 You genius Works on your version, just have to addapt it now. Thank you utexas_pjm Link to comment https://forums.phpfreaks.com/topic/57872-solved-text-replace-from-array/#findComment-286823 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.