本文介绍了如何将str_replace()与多个参数一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想同时将$city_name$ref_name与str_replace()一起使用,以便如果用户输入了其中一个(或两个),则它们都将替换为其实际的变量形式.

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

我基本上希望str_replace('$city_name', $city_name, $headline)成为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/zh-CN/function.str-replace.php

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

这篇关于如何将str_replace()与多个参数一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 03:47