使用 preg_replace 替换单引号之间的所有点字符
Replace all dot characters between single quotes using preg_replace
我有下一个
$pattern = "~'(.*?)\.(.*?)'~i";
$replacement = "'\1\\x2e\2'"
$subject = "window.location.href='example.com'";
preg_replace($pattern, $replacement, $subject);
效果不错,我得到了
"window.location.href='example\x2ecom'"
但如果我有
$subject = "window.location.href='www.example.com'";
或
$subject = "window.location.href='www.example.example.com'";
我在字符串中有点。
请帮忙$replacement
更新:
我需要获取 ''
中所有点应该是 \x2e
的字符串
如果我有
"window.location.href='www.example.example.com'"
那我需要
"window.location.href='www\x2eexample\x2eexample\x2ecom'"
一种选择是使用正前瞻和否定字符 class 以便仅匹配 '
字符之间字符串末尾的文字 .
字符:
$pattern = "~\.(?=[^']*'$)~i";
解释:
\.
- 按字面匹配 .
字符。
(?=
- 正面前瞻的开始。
[^']*'$
- 按字面匹配前面的字符 .
,如果它后面跟着零个或多个非 '
字符,后面跟着 '
字符串。
)
- 正超前结束。
换句话说,只会匹配字符串末尾 '
个字符之间的 .
个字符。
$pattern = "~\.(?=[^']*'$)~i";
$replacement = "\x2e";
$subject = "window.location.href='www.example.com'";
echo preg_replace($pattern, $replacement, $subject);
输出:
"window.location.href='www\x2eexample\x2ecom'"
根据您的以下评论:
如果子字符串 window.location.href='.*'
多次出现,那么您可以使用交替 (?:$|;)
以便它匹配到最后一个 ;
或字符串的末尾, $
.
$pattern = "~\.(?=[^']*'(?:$|;))~i";
$pattern = "~\.(?=[^']*'(?:$|;))~i";
$replacement = "\x2e";
$subject = "window.location.href='.www.test.test.com.'; window.location.href='.www.test.test.com.';";
echo preg_replace($pattern, $replacement, $subject);
输出:
"window.location.href='\x2ewww\x2etest\x2etest\x2ecom\x2e'; window.location.href='\x2ewww\x2etest\x2etest\x2ecom\x2e';"
这是一个使用 preg_replace_callback
而不是 preg_replace
的想法。它允许您传入一个函数来对每个匹配项进行操作。
$subject = "window.location.href='example.com.com.com.com.com'";
$subject = preg_replace_callback("~(?<=')(.*?)(?=')~", function($m) {return preg_replace('~\.~', '\x2e', $m[1]);}, $subject);
print $subject;
这将输出:
window.location.href='example\x2ecom\x2ecom\x2ecom\x2ecom\x2ecom'
基本上我们在这里所做的是匹配刻度内的所有内容。然后,当它找到匹配项时,它会将每个点换成该字符串中的 \x2e
。
这是一个工作演示:
我有下一个
$pattern = "~'(.*?)\.(.*?)'~i";
$replacement = "'\1\\x2e\2'"
$subject = "window.location.href='example.com'";
preg_replace($pattern, $replacement, $subject);
效果不错,我得到了
"window.location.href='example\x2ecom'"
但如果我有
$subject = "window.location.href='www.example.com'";
或
$subject = "window.location.href='www.example.example.com'";
我在字符串中有点。
请帮忙$replacement
更新:
我需要获取 ''
中所有点应该是 \x2e
如果我有
"window.location.href='www.example.example.com'"
那我需要
"window.location.href='www\x2eexample\x2eexample\x2ecom'"
一种选择是使用正前瞻和否定字符 class 以便仅匹配 '
字符之间字符串末尾的文字 .
字符:
$pattern = "~\.(?=[^']*'$)~i";
解释:
\.
- 按字面匹配.
字符。(?=
- 正面前瞻的开始。[^']*'$
- 按字面匹配前面的字符.
,如果它后面跟着零个或多个非'
字符,后面跟着'
字符串。)
- 正超前结束。
换句话说,只会匹配字符串末尾 '
个字符之间的 .
个字符。
$pattern = "~\.(?=[^']*'$)~i";
$replacement = "\x2e";
$subject = "window.location.href='www.example.com'";
echo preg_replace($pattern, $replacement, $subject);
输出:
"window.location.href='www\x2eexample\x2ecom'"
根据您的以下评论:
如果子字符串 window.location.href='.*'
多次出现,那么您可以使用交替 (?:$|;)
以便它匹配到最后一个 ;
或字符串的末尾, $
.
$pattern = "~\.(?=[^']*'(?:$|;))~i";
$pattern = "~\.(?=[^']*'(?:$|;))~i";
$replacement = "\x2e";
$subject = "window.location.href='.www.test.test.com.'; window.location.href='.www.test.test.com.';";
echo preg_replace($pattern, $replacement, $subject);
输出:
"window.location.href='\x2ewww\x2etest\x2etest\x2ecom\x2e'; window.location.href='\x2ewww\x2etest\x2etest\x2ecom\x2e';"
这是一个使用 preg_replace_callback
而不是 preg_replace
的想法。它允许您传入一个函数来对每个匹配项进行操作。
$subject = "window.location.href='example.com.com.com.com.com'";
$subject = preg_replace_callback("~(?<=')(.*?)(?=')~", function($m) {return preg_replace('~\.~', '\x2e', $m[1]);}, $subject);
print $subject;
这将输出:
window.location.href='example\x2ecom\x2ecom\x2ecom\x2ecom\x2ecom'
基本上我们在这里所做的是匹配刻度内的所有内容。然后,当它找到匹配项时,它会将每个点换成该字符串中的 \x2e
。
这是一个工作演示: