簡體   English   中英

str_re在A標簽的name屬性中用連字符替換空格

[英]str_replace spaces with hyphens in A tag's name attribute

$ string = preg_replace(“#[name =([[a-zA-Z0-9 .-] +)*]#”,''。“ $ 1”,$ string);

腳本的這一部分無效:

str_replace(' ', '-', "$1")

我需要更換“”用“ - ”,我也嘗試preg_replace主內preg_replacestr_ireplace

但這仍然行不通

替換是預先評估的,而不是每次替換都評估。 但是您可以通過在正則表達式中使用e修飾符來實現

$string = preg_replace("#\[name=([a-zA-Z0-9 .-]+)*]#e", '"<td><a href=\"$front_page/".str_replace(" ", "-", "$1")."\">$1</a></td>"', $string);

或使用preg_replace_callback

function callbackFunction($match) {
    global $front_page;
    return '<td><a href="'.$front_page.'/'.str_replace(" ", "-", $match[1]).'">'.$match[1].'</a></td>';
}
$string = preg_replace_callback("#\[name=([a-zA-Z0-9 .-]+)*]#", 'callbackFunction', $string);

我猜您將必須分兩個步驟進行操作,因為$1不能在str_replace() $1實際上並不作為變量存在,它只是替換字符串中的占位符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM