首先,您應該使用strpos
方法迭代所有出現的事件,使用lastPos
作爲偏移量。然後插入串/新的鏈接,找到收盤
$needle = "website.com?"; // word to find
$endLink = "</a>"
$lastPos = 0;
$str_to_insert = "<br><a href=\"website.com?bla\">New link here</a>" // text to append
while (($lastPos = strpos($mystring, $needle, $lastPos))!== false) {
//position just after finding the string is: occurrence + string length
$lastPos = $lastPos + strlen($needle);
//finding the end of link (to append it there)
$writePos = strpos($mystring, $endLink, $lastPos);
//appending the string and updating $mystring
$mystring = substr_replace($mystring, $str_to_insert, $writePos, strlen($endLink);
//add the appended string to lastPos, to avoid searching it
$lastPos = $writePos + strlen($str_to_insert)
}
編輯
製作$str_to_insert
動態後:
while (($lastPos = strpos($mystring, $needle, $lastPos))!== false) {
$str_to_insert = substr($mystring, $lastPos, len($needle)
//position just after finding the string is: occurrence + string length
$lastPos = $lastPos + strlen($needle);
// ... the rest keeps the same
}
我沒有得到你的問題... – Blueblazer172
我已經編輯問題 – prav