I am trying to get rid of - in a string like 50-000 and output 50000 but unable to using strstr. Here is the code I tried
$addr = "50-000";
echo $addr = strtr($addr, "-", "");
Any clues whats going wrong here.
Thanks.
I am trying to get rid of - in a string like 50-000 and output 50000 but unable to using strstr. Here is the code I tried
$addr = "50-000";
echo $addr = strtr($addr, "-", "");
Any clues whats going wrong here.
Thanks.
try this
echo str_replace("-","","50-000");
According to the manual:
string strtr ( string $str , string $from , string $to )
If given three arguments, ... If from and to have different lengths, the extra characters in the longer of the two are ignored. The length of str will be the same as the return value's.
Since "-"
is longer than ""
, the character is ignored.
As this three-arguments call will return the same string length, it may not help you to shorten the string from "50-000" to "50000".
Instead, you may try the two-arguments call, where the returned string length may differ:
string strtr ( string $str , array $replace_pairs )
E.g.
$addr = "50-000";
echo $addr = strtr($addr, array("-"=>""));
// returns "50000"
In case you are interested to know more about strtr()
and str_replace()
:
PHP strtr vs str_replace benchmarking
When to use strtr vs str_replace?