Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
702 views
in Technique[技术] by (71.8m points)

php - How do I use str_replace() with multiple parameters?

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 my 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...