Jump to content

[SOLVED] replace problams .


redarrow

Recommended Posts

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

 

 

 

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

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.

 

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";

?>

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 ?.

 

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

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.