基本上,我想同时使用带有str_replace()的$city_name和$ref_name,这样如果用户输入其中一个(或两个),则它们都将被实际的变量形式替换.
为了更好地说明,这是我目前的代码;
$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’,$headline)成为str_replace(‘$city_name’AND / OR’$ref_name’,$city_name AND / OR $ref_name,$headline).
任何答案或评论将不胜感激!
http://php.net/manual/en/function.str-replace.php
原文链接:https://www.f2er.com/php/139148.html$searches = array('$city_name','$ref_name'); $replacements = array($city_name,$ref_name); echo str_replace($searches,$replacements,$headline);