Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php how to make a if str_replace?

$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.

like image 406
fish man Avatar asked Jun 05 '11 19:06

fish man


1 Answers

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;
    }
}
like image 176
BoltClock Avatar answered Oct 20 '22 19:10

BoltClock