JDavies
JDavies

Reputation: 2770

echo a variable inside a php string replace

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.

Upvotes: 1

Views: 136

Answers (3)

Patrick
Patrick

Reputation: 922

$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.

Upvotes: 1

becquerel
becquerel

Reputation: 1131

have a look at PHP String Operators.

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

Upvotes: 4

Adam
Adam

Reputation: 1371

I think You have to do this:

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

Upvotes: 4

Related Questions