2

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!

5
  • Duplicate of str_replace() function and multiple variables? Commented Aug 10, 2011 at 19:39
  • 1
    Please do not ask the same question more than once. You were told exactly how to do this 11 hours ago. It's insulting to the person that already helped you (to whom you've given no feedback, no votes, nor accepted his answer) and it's insulting to the people you're now asking to write the same code you've already been given. I will not be helping you in the future. Commented Aug 10, 2011 at 19:40
  • It's not really a duplicate. The answer he'd given got me to the code above, but he really didn't answer my question as you did with much more concise code, thus I wasn't told "exactly" how to do this 11 hours ago. Nevertheless, thanks for your help. Commented Aug 10, 2011 at 19:48
  • No, his code replaces all your placeholders in the text, without even having to hard code them like this. He answered this question better than I did, 11 hours ago. Commented Aug 10, 2011 at 19:51
  • Well, excuse my ignorance :(. As you may have noticed, I'm new to PHP, and what I saw in his code was a lot of overheard (for a newbie), an if statement at the end which I thought was un-needed, variable variables that I really didn't understand the use of etc. Indeed, I should have commented on answer, and I accept it being insulting to not do so. Commented Aug 10, 2011 at 20:02

1 Answer 1

5

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);
Sign up to request clarification or add additional context in comments.

1 Comment

What is $headline? What is its purpose?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.