簡體   English   中英

如何在php中使用正則表達式替換字符串中的值?

[英]How to replace value in string using regular expression in php?

這里我的字符串($commit$ + $Bug$)/$HR$*($Leader$^$IT$) ...我想用1替換所有$variable$ replace ...

喜歡(1 + 1)/1*(1^1) ......

是否有可能替換值1 ??? 怎么樣??

不要在乎$ _ _ $之間的哪個變量...

請幫我...

$result = preg_replace(
    '/\$ # Match $
    \w+  # Match one or more alphanumeric characters
    \$   # Match $/x', 
    '1', $subject);

這假設只有字符[A-Za-z0-9_]$$之間是合法的。

試試這個吧

\$(.*?)*\$  Or (\$\w*\$)+
preg_replace('/\$(.*?)*\$/i', '1', '($commit$ + $Bug$)/$HR$*($Leader$^$IT$)');

1] 點擊這里

2] 更好的一個

檢查以上鏈接以獲得答案

$str = '($commit$ + $Bug$)/$HR$*($Leader$^$IT$)';
$str = preg_replace('/\$(.*?)\$/', '1', $str);
echo $str;

暫無
暫無

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

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