Jump to content

code problam str_relace and others....


redarrow

Recommended Posts

only the word sister should be changed from the varable $word but it all goes xxx

why please intreasting..

<?php

$word="i got a sister";

$x=explode(' ',$word);

foreach($x as $r){

$a=array("mum","dad","sister","brother");

if(in_array("$r",$a)){

$d=str_replace($a,"xxx",$r);

echo $d;
}
}
?>

Link to comment
Share on other sites

tried this aswell but stil xxx but the word only from the sentemce for sister suppose to change not the rest off the sentence....

 

result should be

 

i got a xxx

 

<?php

$word=$_POST['word'];

$word="i got a sister";

$x=explode(' ',$word);

foreach($x as $r){

$a=array("mum","dad","sister","brother");

foreach($a as $b){

if($b==$r){	

$d=str_replace($b,"xxx",$r);

echo $d;
}
}
  }
?>

Link to comment
Share on other sites

Your code will output xxx because you are not saving the changes back to the original string. You seem to be confused with how foreach works.

 

str_replace can take an array of replacement words to be replaced by a single string, Your code can be simplified to just:

$words = array("mum","dad","sister","brother");
$string="i got a sister";

echo str_replace($words, 'xxx', $string); // result: i got a xxx

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.