[英]How to add parenthesis to the caputures of the regex pattern in php?
我想為字符串內的變量添加括號。 我怎樣才能使用正則表達式在 php 上做到這一點?
字符串: UPDATE item_tbl SET name = '$name',code = '$code',category_fk = $category_fk,modified_by_fk = $modified_by_fk,price = $price
正則表達式: /\\$(\\w*)/g
輸出 :
UPDATE item_tbl SET name = '($name)',code = '($code)',category_fk = ($category_fk),modified_by_fk = ($modified_by_fk),price = ($price)
你可以試試這個:
(\$\w+)
並替換為:
($1)
示例解決方案(在此處運行)
$re = '/(\$\w+)/';
$str = 'UPDATE item_tbl SET name = \'$name\',code = \'$code\',category_fk = $category_fk,modified_by_fk = $modified_by_fk,price = $price';
$subst = '($1)';
$result = preg_replace($re, $subst, $str);
echo $result;
示例輸出:
UPDATE item_tbl SET name = '($name)',code = '($code)',category_fk = ($category_fk),modified_by_fk = ($modified_by_fk),price = ($price)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.