0
我想將外觀段落文本轉換爲錨鏈接。preg_replace insade paragprah添加錨鏈接
$change = array(
'google' => 'www.google.com',
'facebook' => 'www.facebook.com',
);
$text = "
<h1>Search on google for facebook</h1>
<p>Search on google for facebook</p>
";
foreach ($change as $word => $url) {
$sentence = preg_replace('@(?<=\W|^)('.$word.')(?=\W|$)@i', '<a href="'.$url.'">$1</a>', $text);
}
echo $sentence;
我想這個堅決:
<h1>Search on google for facebook</h1>
<p>Search on <a href="www.google.com">google</a> for <a href="www.facebook.com">facebook</a></p>
嘗試https://ideone.com/j9XqSd –
非常感謝! – lilly321