开发者

How do I use str_replace() with multiple parameters?

开发者 https://www.devze.com 2023-03-27 18:43 出处:网络
Basically, I want to use both $city_name and $ref_name with str_replace(), so that if either one (or both) of them are inputted by the user, both of them are replaced with their actual variable forms.

Basically, I want to use both $city_name and $ref_name with str_replace(), so that if either one (or both) of them are inputted by the user, both of them are replaced with their actual variable forms.

To give a better illustration, here's m开发者_Go百科y current code;

$headlines = array('primary_headline' => $_POST['primary_headline'], 'secondary_headline' => $_POST['secondary_headline'], 'primary_subline' => $_POST['primary_subline'], 'secondary_subtext' => $_POST['secondary_subtext']);
$city_name = "Dallas";
$ref_name = "Facebook";
if(isset($headlines)) {
  foreach($headlines as $headline) {
      echo(str_replace('$city_name', $city_name, $headline));
  }
}

I'd basically like str_replace('$city_name', $city_name, $headline) to become str_replace('$city_name' AND/OR '$ref_name', $city_name AND/OR $ref_name, $headline).

Any answers or comments will be greatly appreciated!


http://php.net/manual/en/function.str-replace.php

$searches = array('$city_name', '$ref_name');
$replacements = array($city_name, $ref_name);
echo str_replace($searches, $replacements, $headline);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号