Forum Moderators: coopster

Message Too Old, No Replies

how to simplify str replace

str_replace

         

mustdownloads

7:27 am on Mar 4, 2008 (gmt 0)

10+ Year Member



Dear Friends,

I need one small help.

I wish to change a,s,g,j in certain text to z.

I am using this code.

Is there a simple way, so insteat of four lines i can use one line.

<?php
$st = "example asdfghjkl";

$st = str_replace("a", "z", $st);
$st = str_replace("s", "z", $st);
$st = str_replace("g", "z", $st);
$st = str_replace("j", "z", $st);

echo "$st";
?>

Regards

ayushchd

10:22 am on Mar 4, 2008 (gmt 0)

10+ Year Member



$st = "example asdfghjkl";
$letters = array('a', 's', 'g', 'j');
foreach ($letters as $val) {
$st = str_replace($val, "z", $st);
}

dreamcatcher

10:33 am on Mar 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



str_replace supports arrays. No loop needed.

$letters = array('a', 's', 'g', 'j');
$st = str_replace($letters,'z',$st);

In a single line:
$st = str_replace(array('a', 's', 'g', 'j'),'z',$st);

dc

mustdownloads

11:49 am on Mar 4, 2008 (gmt 0)

10+ Year Member



thank you dreamcatcher

ayushchd

12:09 pm on Mar 4, 2008 (gmt 0)

10+ Year Member



hey tnx dc..

and mustdownloads, don't I deserve a thank you? lol..juss kidding ;)

dreamcatcher

12:34 pm on Mar 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No problem Guys.

dc

mustdownloads

12:43 pm on Mar 4, 2008 (gmt 0)

10+ Year Member



sorry ayushchd,

really sorry, anyway one more issue
if wish to have different results for each variable, can we do it in single line.

<?php
$st = "example asdfghjkl";

$st = str_replace("a", "b", $st);
$st = str_replace("s", "f", $st);
$st = str_replace("g", "t", $st);
$st = str_replace("j", "u", $st);

echo "$st";
?>

Receptional Andy

12:51 pm on Mar 4, 2008 (gmt 0)



As per the str_replace manual [php.net]:

// Provides: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);

ayushchd

4:05 pm on Mar 4, 2008 (gmt 0)

10+ Year Member



$st = "example asdfghjkl";
$letters = array('a' => 'b', 's' => 'f', 'g' => 't', 'j' => 'u');
foreach ($letters as $match => $replace)
$st = str_replace($match, $replace, $st);
echo $st;

dtest

9:17 pm on Mar 4, 2008 (gmt 0)

10+ Year Member



even more simple:

<?php
$st = "example asdfghjkl";

$st = str_replace(array("a", "s", "g", "j"), array("b", "f", "t", u"), $st);

echo "$st";
?>

Receptional Andy

9:29 pm on Mar 4, 2008 (gmt 0)



It's a bit off topic, but remember that shorter code does not mean better code.

Especially if you are new to a language, shortening code too much will just make it harder to maintain, and more difficult to understand, especially if you need to go back to it in future. (Incidentally, you could fit the first $st on the same line, if you wanted to, dtest ;))

I forget whose anecdote it is, but it goes something like this (help me out with the real source, someone!):

The programming student takes his work to the professor, and tells him that something in his code isn't working.

The professor says "of course it isn't working, there aren't any comments in it!".

dtest

9:48 pm on Mar 4, 2008 (gmt 0)

10+ Year Member



good point, Receptional Andy

however in this case the topic author requested a simpler code: "how to get 4 lines down to 1" :)

ayushchd

6:10 pm on Mar 5, 2008 (gmt 0)

10+ Year Member



dtest..

I think my code will be easier to add new pairs of letters and easily make correspondences :)

mustdownloads

1:09 pm on Mar 6, 2008 (gmt 0)

10+ Year Member



My code has 1700 variables to replace, i compared these two things for that.
it was surprising to discover that code one (without loop) take significantly less time in comparison to second.

code one
// Provides: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);

code two
$st = "example asdfghjkl";
$letters = array('a' => 'b', 's' => 'f', 'g' => 't', 'j' => 'u');
foreach ($letters as $match => $replace)
$st = str_replace($match, $replace, $st);
echo $st;

jatar_k

2:08 pm on Mar 6, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



why were you surprised?

the first code searches your string once and makes replacements

the second does the same thing 4 times

my thought is that making 4 replacements on a single search would take longer than a single replacement per search but the search is what takes the time. So you your first code should be less than 4 times faster than the second.