Question:
How to shorten this PHP script? (Involves string_replace)?
peter s
2007-02-02 06:56:20 UTC
$replacewith = "";
$replace = "¬";
$new = str_replace
("$replace",
"$replacewith",
"$toast");

$replacewith = "";
$replace = "~";
$new = str_replace
("$replace",
"$replacewith",
"$toast");

The first 3 lines check to see if the variable toast has any "¬" characters in it and them deletes them. The second 3 lines check for this symbol "~" and does the same. I was just wondering if it was possible to put this script all in one str_replace so it replaces all the unwanted characters in one script, instead of doing the str replace function for every single unwanted character.
Three answers:
sofarsogood
2007-02-02 07:10:36 UTC
sure, use preg_replace(("~", "¬"), (), $toast)



http://www.php.net/manual/en/function.preg-replace.php
barile
2016-09-28 11:54:52 UTC
anybody is familiar with that IPL is a midsection of adventure fixing and corruption. So there should not be any marvel by modern-day spot fixing scandal and Sreesanth or 2 different cricketers are actually not remoted case. what is going to you're saying some enormous no ball by RP Singh or 3 catches dropped by Pollord in a row. Why, even in the day gone by's adventure between Rajasthan and Hyderabad, gamers of Rajasthan did not choose for run out allure in a sparkling run out. IPL is all approximately vulgar funds. We purpose audience have our daily restoration of cricket-entertainment. not in basic terms there is spot fixing yet in addition suits are scripted and that's pre desperate which communities are going to play off around and which group is going to win the trophy.
the DtTvB
2007-02-02 08:55:22 UTC
By using the same method (str_replace)

you can shorten the code into just 2 lines!




$toast = 'hello¬ ~haha';

$new = str_replace(array('¬', '~'), '', $toast);

echo $new;

?>


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...