2013-10-08 38 views
1

我目前正在嘗試在PHP中對我來說是新的str_replace。這是我目前的代碼。回聲一個PHP字符串內的變量替換

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

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

然而,在那裏我有「我的新聞」上面的網址我想改變這種狀況,因此輸出存儲在管理的URL。因此,我不想爲每個類別做一個if語句,而是將它作爲變量輸出。

這裏是我一直試圖做的:

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

我希望這是足夠的信息。任何幫助將非常感激。

+0

串聯與'.'運算符 – bwoebi

回答

4

我認爲你必須這樣做:

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

它的工作!謝謝! – JDavies

+0

@JoshDavies我很高興我能幫上忙。不要忘記接受答案:) – Adam

+0

我不會擔心,這是該死的10分鐘倒計時! :) 再次感謝! – JDavies

4

看看PHP String Operators

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

我會接受另一個答案,因爲他是第一個。但嚴重的是,謝謝! +1額外信息! – JDavies

+0

你這樣做 - 他確實是第一次:) – becquerel

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

這是你想要的嗎?使用 。用於連接,不要回顯變量。