我有值和鏈接的數組,並需要在與該鏈接的內容替換的確切值只有一次。 對於此的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://」正常工作...... 現在,這是受到質疑,怎麼逃生呢?
非常感謝...真的棘手:) –