2013-06-20 40 views
0

我有值和鏈接的數組,並需要在與該鏈接的內容替換的確切值只有一次。 對於此的preg_replace可以幫助像:確切使用單詞替換陣列

Array ( 
[0] => Array ([keyword] => this week [links] => http://google.com) 
[1] => Array ([keyword] =>this [links] => http://yahoo.com) 
[2] => Array ([keyword] => week [links]=> http://this-week.com)) 
) 

的文字是:

$content = "**This week** I'd like to go to the seaside, but the weather is not good enough. Next **week** will be better. **This** is a kind of great news.**This week** I'd like to go to the seaside, but the weather is not good enough. Next **week** will be better. **This** is a kind of great news.**This week** I'd like to go to the seaside, but the weather is not good enough. Next **week** will be better. **This** is a kind of great news."; 

我試圖用繩子做更換 - 爲陣列可用於字符串替換,但後來所有的出現次數被替換。 我試圖用substr_replace,有位置,但它不工作,我想是。

$pos = strpos($content,$keyword); 
if($pos !== false){ 
    substr_replace($content,$link,$pos,strlen($keyword)); 
} 

和preg_replace函數,用循環數組:

preg_replace('/'.$keyword.'/i', $link, ($content),1); 

這是一種工作,它會替換一次與鏈接的關鍵字,但是如果關鍵字(本週)的化合物,它本週,這是錯誤的...

希望你的幫助......謝謝。

更新

$環節是問題 - 如果它是沒有的「http://」正常工作...... 現在,這是受到質疑,怎麼逃生呢?

回答

1

如果你會,而不是鏈接使用某種類型的「ID」的,在preg_replace函數使用它,當做到這一點,你可以調用str_replace函數與實際的聯繫取代IDS?

$content = preg_replace('/'.$keyword.'/i', $IDS, ($content),1); 
$content = str_replace($IDS, $link, $content); 
+0

非常感謝...真的棘手:) –

1

我編寫了代替所有值的代碼示例。我不知道你想要所以我把樣品文本完全相同的文本被替換什麼

$content = "**This week** I'd like to go to the seaside, but the weather is not good enough. Next **week** will be better. **This** is a kind of great news.**This week** I'd like to go to the seaside, but the weather is not good enough. Next **week** will be better. **This** is a kind of great news.**This week** I'd like to go to the seaside, but the weather is not good enough. Next **week** will be better. **This** is a kind of great news."; 
$replacement = array('[linkthiswWeek]', '[links_this]','[links_Week]'); 
$patterns = array('/This week/', '/This/', '/week/'); 
echo preg_replace($patterns, $replacement, $content); 

OUTPUT:

**[linkthiswWeek]** I'd like to go to the seaside, but the weather is not good enough. Next **[links_Week]** will be better. **[links_this]** is a kind of great news.**[linkthiswWeek]** I'd like to go to the seaside, but the weather is not good enough. Next **[links_Week]** will be better. **[links_this]** is a kind of great news.**[linkthiswWeek]** I'd like to go to the seaside, but the weather is not good enough. Next **[links_Week]** will be better. **[links_this]** is a kind of great news. 

您可以通過對矯正$replacement陣列更換適合您的需要

+0

感謝,羅伯特...我測試你的榜樣和工作正常,但如果我實現這個在我的代碼不會:(它取代「本週」與鏈接「這個」和「周」。我試圖通過手動方式使陣列,而不是從數據庫調用,但是不能幫助......真的不知道有什麼可以是錯誤的 –

1

羅伯特的答案的作品,但代替每一次出現,這是不需要的。在preg替換結尾添加',1'只會替換一次。

例如echo preg_replace($ patterns,$ replacement,$ content,1);

+0

是,羅伯特的回答工作 - 但不是在我的代碼...我實現了相同的陣列,但separatelly替換每個字 - 其實我是想之前做到這一點...也許是有毛病的內容 - 打破更換 - 就可以了,什麼..hm如果我添加其他關鍵字,短,但類似的,讓我們說'thi'並且鏈接'thi.com',它用最後一個添加替換'this'。 –

+0

Roberts代碼的工作原理是因爲他使用了2個數組,一個用於您正在搜索的內容,另一個用於替換它。我認爲你的數組陣容會讓你想要達到的目標變得複雜。此外,我們在此討論字符串替換,因此搜索字段和替換應該是字符串。 –

+0

好吧,我用簡單的例子..我測試了這麼多小時,我無法弄清楚發生了什麼。現在正在研究LoremIpsum文本,工作正常,但在我的文本上 - 簡單的一個,使用簡單的數組,基於完全相同的方式手動構建,不起作用...它使我瘋狂:( –