Jump to content

Using str_ireplace


RobberBaron

Recommended Posts

I have a PHP script which is supposed to act as a translator. It saves $_POST['msg'] (the posted message to translate) in variable $b. It then uses PHP's str_ireplace to edit variable $b before sending it back to the page. When it uses str_ireplace it replaces an occurrence of str_ireplace so that I can replace ' string' and 'string ', rather than ' string ' to stop cause problems for words that are also letters like 'i'. It's similar to this:

<?php$a=$_POST;$b=$a['msg'];$b=str_ireplace(' i',' uh',str_ireplace('i ','uh ',$b));?><html><head><title>Kaboom - RobberBaron Translate</title></head><body><form action="<?php Echo($_SERVER['PHP_SELF']); ?>" method="post"><input type="hidden" name="con[1]" id="con[1]" value="-1" /><textarea name="msg" id="msg" rows="5" cols="50"><?php Echo($b); ?></textarea><input type="submit" value="Translate" accesskey="t" /> | <input type="reset" value="Clear" accesskey="c" /> | <input type="button" value="Refresh" onclick="window.location='translate.php';" accesskey="r" /><br /><fieldset><legend style="color:#0000FF;font-weight:bold;">Result:</legend><textarea rows="5" cols="50"><?php Echo($b); ?></textarea></fieldset></form></body></html>

This should work fine, however I invented a new function to make adding new words quicker called c. Function c() takes one argument, which explodes the string and uses the array items for str_ireplace. This is what the code looks like:

<?php$a=$_POST;$b=$a['msg'];Function c($d){$e=explode(' ',$d);$b=str_ireplace($e[0].' ',$e[1].' ',str_ireplace(' '.$e[0],' '.$e[1],$b));};c('i uh');?><html><head><title>Kaboom - RobberBaron Translate</title></head><body><form action="<?php Echo($_SERVER['PHP_SELF']); ?>" method="post"><input type="hidden" name="con[1]" id="con[1]" value="-1" /><textarea name="msg" id="msg" rows="5" cols="50"><?php Echo($b); ?></textarea><input type="submit" value="Translate" accesskey="t" /> | <input type="reset" value="Clear" accesskey="c" /> | <input type="button" value="Refresh" onclick="window.location='translate.php';" accesskey="r" /><br /><fieldset><legend style="color:#0000FF;font-weight:bold;">Result:</legend><textarea rows="5" cols="50"><?php Echo($b); ?></textarea></fieldset></form></body></html>

When I print_r() the array e the elements show up fine, but for some reason str_ireplace doesn't affect $b and it just prints the same message that was sent. I don't understand the problem, can you take a look please? Also, my code is messy to save file space - it was messier than that before I put it into this post (you should see my sock drawer).

Link to comment
Share on other sites

The search string is the posted message. I'm only using explode to make a quicker way of adding translate items rather than using different arguments, I add spaces to the str_ireplace() so that if someone posted "I didn't think I could", it would get the first "I " as $e[0].' ', the third " I " with ' '.$e[0] but not the second "i" in "didn't".

Link to comment
Share on other sites

But if you explode the string "I didn't think I could" on spaces, you're going to get an array with these elements:"I""didn't""think""I""could"Notice that none of them have spaces around the values. Explode already removed the spaces. You're not going to find spaces if you're searching for them.

Link to comment
Share on other sites

I'm not exploding that string, that is just the posted message from the user stored in $b ($b=$_POST['msg']). The only thing exploded is in the function c($d), where $d is exploded to get what to find and what to replace it with, which is later parsed with str_ireplace() and that's when the spaces are added to compare with $_POST['msg']. This is the order of events for the second code:Translate.click();POST(_DATA)Exploding Function c() constructed - not calledstring("i uh") split into array $eCall str_ireplace($e[0]=>" i",$e[1]=>" uh",$_POST['msg']) returned to call str_ireplace($e[0]=>"i ",$e[1]."uh ",{RETURNED_DATA FROM LAST str_ireplace});Finalised result sent to page

Link to comment
Share on other sites

The function is trying to use a global variable that is not global, $b is not defined in the function (what's with all the single-letter names, anyway? It helps to choose descriptive names). You can either have the function use the global variable or pass that value to the function also.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...