redarrow Posted April 12, 2009 Share Posted April 12, 2009 advance thank you. i want to replace all in the array with the sentence match. <?php $sentence="hi the < would ? + like to <? php say hello"; $words=array("<",">","<>","?","+","<?php","?>","javascript"); for($i=0; $i<count($words); $i++){ $result=str_replace($words[$i],"xxx",$sentence); echo $result; break; } ?> current result. hi the xxx would ? + like to xxx? php say hello ps. it like it ignoring, some information, and giving the wrong result. Link to comment https://forums.phpfreaks.com/topic/153731-solved-replace-problams/ Share on other sites More sharing options...
redarrow Posted April 12, 2009 Author Share Posted April 12, 2009 bit closer. <?php $sentence="hi the < would > ? + like to <?php say hello"; $sentence=explode(' ',$sentence); $words=array("<",">","<>","?","+","<?php","?>","javascript"); $result=str_replace($words,"xxx",$sentence); foreach($result as $res){ echo "$res\n"; } ?> result. hi the xxx would xxx xxx xxx like to xxxxxxphp say hello still not right. <?php $sentence="hi the < would > ? + like to <? php say hello"; $sentence=explode(' ',$sentence); $words=array("<",">","\<>","\?","\+","<?php","?>","javascript"); $result=str_replace($words,"xxx",$sentence); foreach($result as $res){ echo $res; } ?> result. hithexxxwouldxxx?+liketoxxx?phpsayhello Link to comment https://forums.phpfreaks.com/topic/153731-solved-replace-problams/#findComment-807882 Share on other sites More sharing options...
thebadbad Posted April 12, 2009 Share Posted April 12, 2009 So you want to replace the strings in the array with 'xxx', in $sentence? <?php $sentence = 'hi the < would ? + like to <? php say hello'; $words = array("<", ">", "<>", "?", "+", "<?php", "?>", "javascript"); $result = str_replace($words, 'xxx', $sentence); echo $result; ?> gives me hi the xxx would xxx xxx like to xxxxxx php say hello as expected. Link to comment https://forums.phpfreaks.com/topic/153731-solved-replace-problams/#findComment-807889 Share on other sites More sharing options...
redarrow Posted April 12, 2009 Author Share Posted April 12, 2009 so i need to add the word php to get this >>><?php changed as one? example. <?php $sentence="hi <> the < would > ? + like javascript to <?php say ?> hello"; $words=array("<",">","<>","?","+","<?php","?>","javascript","php"); $result=str_replace($words,"xxx",$sentence); echo "$result\n"; ?> Link to comment https://forums.phpfreaks.com/topic/153731-solved-replace-problams/#findComment-807893 Share on other sites More sharing options...
thebadbad Posted April 12, 2009 Share Posted April 12, 2009 I'm not sure what you mean, but you probably need to understand that in the part <?php, first < is replaced, then ?, and when preg_replace() searches for <?php, it's not there anymore. Try rearranging the elements in $words, in order of priority. I.e. putting <?php before < and ?. Link to comment https://forums.phpfreaks.com/topic/153731-solved-replace-problams/#findComment-807896 Share on other sites More sharing options...
redarrow Posted April 12, 2009 Author Share Posted April 12, 2009 one more question, is it better to get rid off the words or use xxx what the normal preference. works cheers. solved. <?php $sentence="hi there i love <?php yee haaa ?>"; $words=array("<?php","?>"); $result=str_replace($words,"xxx",$sentence); echo "$result\n"; ?> result via adding the <?php and ?> beginning of the array. hi there i love xxx yee haaa xxx Link to comment https://forums.phpfreaks.com/topic/153731-solved-replace-problams/#findComment-807897 Share on other sites More sharing options...
thebadbad Posted April 12, 2009 Share Posted April 12, 2009 Really depends on what you're using this for. Link to comment https://forums.phpfreaks.com/topic/153731-solved-replace-problams/#findComment-807898 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.