1

I currently trying to do a str_replace in PHP which is new to me. Here is my current code.

$postAddress = str_replace("/current-url/", "/path/to/my-news/post/", $postAddress);

<li><a href="<?php echo $postAddress; ?>" ><?php echo $post->getTitle(); ?></a></li>

However in the above URL where I have 'my-news' I want to change that so it outputs the URL stored in the admin. So rather than doing an if statement for each category I want to output it as a variable.

Here is what i've tried to do:

$postAddress = str_replace("/current-url/", "/path/to/"echo $cat->getTitle() "/post/", $postAddress);

I hope this is enough info. Any help would be much appreciated.

1
  • concatenation is with . operator Commented Oct 8, 2013 at 11:10

3 Answers 3

4

I think You have to do this:

$postAddress = str_replace("/current-url/", "/path/to/".$cat->getTitle()."/post/", $postAddress);
Sign up to request clarification or add additional context in comments.

2 Comments

I won't don't worry, it's this damn 10 minute countdown! :) Thanks again!
@JoshDavies - 10 minutes have passed ;)
4

have a look at PHP String Operators.

$postAddress = str_replace("/current-url/", "/path/to/" . $cat->getTitle() . "/post/", $postAddress);

2 Comments

I will accept the other answer as he was first. But seriously, thank you! +1 for extra info!
You do that - he was indeed first :)
1
$postAddress = str_replace("/current-url/", "/path/to/" . $cat->getTitle() . "/post/", $postAddress);

Is that what you want? Use . for concatenation and don't echo the variable.

Comments

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.