使用preg_replace替换单引号之间的所有点字符

时间:2022-05-02 22:26:21

I have next

我有下一个

$pattern = "~'(.*?)\\.(.*?)'~i";
$replacement = "'\\1\\\\x2e\\2'"
$subject = "window.location.href='example.com'";
preg_replace($pattern, $replacement, $subject);

It work nice and I got

它工作得很好,我得到了

"window.location.href='example\x2ecom'"

But if I have

但是如果我有

$subject = "window.location.href='www.example.com'";

or

$subject = "window.location.href='www.example.example.com'";

I have dots in string.

我在字符串中有点。

Please help with $replacement

请帮忙替换美元

UPDATE:

更新:

I need get string where all dots in '' should be \x2e

我需要一个字符串,所有的点都应该是\x2e

If I have

如果我有

"window.location.href='www.example.example.com'"

then I need

然后我需要

"window.location.href='www\x2eexample\x2eexample\x2ecom'"

2 个解决方案

#1


2  

One option is to use a positive lookahead and a negated character class in order to only match the literal . characters at the end of the string between the ' characters:

一种选择是使用一个积极的前视和一个否定的字符类,以便只匹配文字。“字符”之间的字符串末尾的字符:

$pattern = "~\\.(?=[^']*'$)~i";

Explanation:

  • \\. - Match the . character literally.
  • \ \。——匹配。字符。
  • (?= - Start of a positive lookahead.
  • (?= -积极展望未来。
  • [^']*'$ - Match the preceding character literally, ., if it is followed by zero or more non-' characters followed by ' at the end of the string.
  • [^ ']* $ -匹配前面的字符,,如果是紧随其后的是零个或多个非字符后跟的末端的字符串。
  • ) - End of the positive lookahead.
  • ) -结束积极的展望。

In other words, only the . characters between ' characters at the end of your string will be matched.

换句话说,只有。字符串末尾的字符之间的字符将被匹配。

Example Here

例子

$pattern = "~\\.(?=[^']*'$)~i";
$replacement = "\\x2e";
$subject = "window.location.href='www.example.com'";
echo preg_replace($pattern, $replacement, $subject);

Output:

输出:

"window.location.href='www\x2eexample\x2ecom'"

Based on your comment(s) below:

根据你的评论:

If there are multiple occurrences of the substring window.location.href='.*', then you could use the alternation (?:$|;) so that it matches up until the last ; or the end of the string, $.

如果子字符串window.location.href='出现多次。*',然后你可以使用alternation(?:$|;),使它匹配到最后;或者字符串的末尾$。

$pattern = "~\\.(?=[^']*'(?:$|;))~i";

Example Here

例子

$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);

Output:

输出:

"window.location.href='\x2ewww\x2etest\x2etest\x2ecom\x2e'; window.location.href='\x2ewww\x2etest\x2etest\x2ecom\x2e';"

#2


1  

Here's an idea using preg_replace_callback instead of preg_replace. It allows you to pass in a function to operate on each match.

这里有一个使用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;

This will output:

这将输出:

window.location.href='example\x2ecom\x2ecom\x2ecom\x2ecom\x2ecom'

Basically what we're doing here is matching everything inside of the ticks. Then, when it finds a match, it swaps out each dot for \x2e within that string.

基本上我们在这里做的是匹配蜱体内的所有东西。然后,当它找到一个匹配的时候,它会在这个字符串中掉出每个点的\x2e。

Here is a working demo:

这里有一个工作演示:

http://ideone.com/eYh2rO

http://ideone.com/eYh2rO

#1


2  

One option is to use a positive lookahead and a negated character class in order to only match the literal . characters at the end of the string between the ' characters:

一种选择是使用一个积极的前视和一个否定的字符类,以便只匹配文字。“字符”之间的字符串末尾的字符:

$pattern = "~\\.(?=[^']*'$)~i";

Explanation:

  • \\. - Match the . character literally.
  • \ \。——匹配。字符。
  • (?= - Start of a positive lookahead.
  • (?= -积极展望未来。
  • [^']*'$ - Match the preceding character literally, ., if it is followed by zero or more non-' characters followed by ' at the end of the string.
  • [^ ']* $ -匹配前面的字符,,如果是紧随其后的是零个或多个非字符后跟的末端的字符串。
  • ) - End of the positive lookahead.
  • ) -结束积极的展望。

In other words, only the . characters between ' characters at the end of your string will be matched.

换句话说,只有。字符串末尾的字符之间的字符将被匹配。

Example Here

例子

$pattern = "~\\.(?=[^']*'$)~i";
$replacement = "\\x2e";
$subject = "window.location.href='www.example.com'";
echo preg_replace($pattern, $replacement, $subject);

Output:

输出:

"window.location.href='www\x2eexample\x2ecom'"

Based on your comment(s) below:

根据你的评论:

If there are multiple occurrences of the substring window.location.href='.*', then you could use the alternation (?:$|;) so that it matches up until the last ; or the end of the string, $.

如果子字符串window.location.href='出现多次。*',然后你可以使用alternation(?:$|;),使它匹配到最后;或者字符串的末尾$。

$pattern = "~\\.(?=[^']*'(?:$|;))~i";

Example Here

例子

$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);

Output:

输出:

"window.location.href='\x2ewww\x2etest\x2etest\x2ecom\x2e'; window.location.href='\x2ewww\x2etest\x2etest\x2ecom\x2e';"

#2


1  

Here's an idea using preg_replace_callback instead of preg_replace. It allows you to pass in a function to operate on each match.

这里有一个使用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;

This will output:

这将输出:

window.location.href='example\x2ecom\x2ecom\x2ecom\x2ecom\x2ecom'

Basically what we're doing here is matching everything inside of the ticks. Then, when it finds a match, it swaps out each dot for \x2e within that string.

基本上我们在这里做的是匹配蜱体内的所有东西。然后,当它找到一个匹配的时候,它会在这个字符串中掉出每个点的\x2e。

Here is a working demo:

这里有一个工作演示:

http://ideone.com/eYh2rO

http://ideone.com/eYh2rO