在php字符串替换中回显一个变量

时间:2013-10-08 11:08:08

标签: php

我目前正在尝试在PHP中使用str_replace,这对我来说是新的。这是我目前的代码。

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

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

但是在上面的网址中,我有'my-news',我想更改它,以便输出存储在admin中的URL。因此,我不想为每个类别执行if语句,而是将其作为变量输出。

以下是我试过的事情:

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

我希望这是足够的信息。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:4)

我想你必须这样做:

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

答案 1 :(得分:4)

查看PHP String Operators

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

答案 2 :(得分:1)

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

这就是你想要的吗?使用 。用于连接而不回显变量。

相关问题