$str
is some value in a foreach.
$str = str_replace('_name_','_title_',$str);
how to make a if str_replace
?
I want do the thing if have a str_replace
then echo $str
, else not, jump the current foreach then to the next. Thanks.
There is a fourth parameter to str_replace()
that is set to the number of replacements performed. If nothing was replaced, it's set to 0. Drop a reference variable there, and then check it in your if statement:
foreach ($str_array as $str) {
$str = str_replace('_name_', '_title_', $str, $count);
if ($count > 0) {
echo $str;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With