Jump to content
MakeWebGames

duplicate help Please grpg


thebobby

Recommended Posts

$subject = str_replace("\\", "\\\\", htmlspecialchars($_POST['subject'], ENT_QUOTES ));

$subject = str_replace("yahoo", " ", htmlspecialchars($_POST['subject'], ENT_QUOTES ));

$msgtext = str_replace(".yahoo", "google ", htmlspecialchars($_POST['msgtext'], ENT_QUOTES ));

$send_person = new User($to);

this works if yahoo is wrote in message box it changes to goolge. But want i want is to be able to add more. Like this:

$subject = str_replace("\\", "\\\\", htmlspecialchars($_POST['subject'], ENT_QUOTES ));

$subject = str_replace("yahoo", " ", htmlspecialchars($_POST['subject'], ENT_QUOTES ));

$msgtext = str_replace(".yahoo", "google ", htmlspecialchars($_POST['msgtext'], ENT_QUOTES ));

$msgtext = str_replace(".ebay", "google ", htmlspecialchars($_POST['msgtext'], ENT_QUOTES ));

$msgtext = str_replace(".coke", "google ", htmlspecialchars($_POST['msgtext'], ENT_QUOTES ));

$msgtext = str_replace(".green", "google ", htmlspecialchars($_POST['msgtext'], ENT_QUOTES ));

and when i add more like that it only does the bottom one not the others... so anybody know how i can write the code so i can have muiltiple words. Wats this does is it replace the first word and makes it google. these words are examples

Link to comment
Share on other sites

Use an array. :)

Edit: To be helpful, an example of a way you could do it.

function replaceWords($text) {
   $words = array (
       'Google' => 'Yahoo',
       'Bing' => 'Duck Duck Go',
       'AltaVista' => 'AWWWWW YEAAAAAAH'
   );

   $replaced = str_ireplace(array_keys($words), array_values($words), $text);
   return htmlentities($replaced, ENT_QUOTES, 'UTF-8');
}

/*
   TEST
*/
$str = 'Google, google, bing, Bing, AltaVista, altavista, AlTaViStA';
echo 'STRING: '.$str.'<br />';
echo 'Replaced: '.replaceWords($str);

/* 
   TEST OUTPUT 

   STRING: Google, google, bing, Bing, AltaVista, altavista, AlTaViStA
   Replaced: Yahoo, Yahoo, Duck Duck Go, Duck Duck Go, AWWWWW YEAAAAAAH, AWWWWW YEAAAAAAH, AWWWWW YEAAAAAAH


*/

On an extra note if you really wanted to do you it your way, make sure after the first you use the correct variables, otherwise it's not going to do all of them (well it will but won't produce the desired result).

Edited by Djkanna
Link to comment
Share on other sites

$msgtext = str_replace(".yahoo", "google ", htmlspecialchars($_POST['msgtext'], ENT_QUOTES ));

$msgtext = str_replace(".ebay", "google ", htmlspecialchars($_POST['msgtext'], ENT_QUOTES ));

$msgtext = str_replace(".coke", "google ", htmlspecialchars($_POST['msgtext'], ENT_QUOTES ));

$msgtext = str_replace(".green", "google ", htmlspecialchars($_POST['msgtext'], ENT_QUOTES ));

Link to comment
Share on other sites

if ($_POST['newmessage'] != "")

{

$to = $_POST['to'];

$from = $user_class->id;

$timesent = time();

$subject = str_replace("yahoo", " ", htmlspecialchars($_POST['subject'], ENT_QUOTES ));

$msgtext = str_replace(".yahoo", "google ", htmlspecialchars($_POST['msgtext'], ENT_QUOTES ));

$send_person = new User($to);

$checkuser = mysql_query("SELECT `id` FROM `grpgusers` WHERE `id`='".$to."'");

$username_exist = mysql_num_rows($checkuser);

this is wat i have

Link to comment
Share on other sites

Then after the first, you would use $msgtext = str_replace('...', '', $msgtext);

But this isn't really a usable way, imagine having 50+ words to replace.

Or use an array within the str_replace();

str_ireplace( array ('Google', 'Yahoo'), array ('Duck Duck Go', 'AltaVista'), $var);

Link to comment
Share on other sites

php.net/str_ireplace

 

$msgtext = str_replace(".yahoo", "google ", htmlspecialchars($_POST['msgtext'], ENT_QUOTES ));
$msgtext = str_replace(".ebay", "google ", htmlspecialchars($_POST['msgtext'], ENT_QUOTES ));
$msgtext = str_replace(".coke", "google ", htmlspecialchars($_POST['msgtext'], ENT_QUOTES ));
$msgtext = str_replace(".green", "google ", htmlspecialchars($_POST['msgtext'], ENT_QUOTES )); 

 

Clearly you have no idea of how php work's. So we will just spoon feed you :D

Use

 

$msgtext = str_replace(".yahoo", "google ", htmlspecialchars($msgtext, ENT_QUOTES ));
$msgtext = str_replace(".ebay", "google ", htmlspecialchars($msgtext, ENT_QUOTES ));
$msgtext = str_replace(".coke", "google ", htmlspecialchars($msgtext, ENT_QUOTES ));
$msgtext = str_replace(".green", "google ", htmlspecialchars($msgtext, ENT_QUOTES )); 
Link to comment
Share on other sites

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.   Paste as plain text instead

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