使用php转义正则表达式中的特殊字符

发布于 2024-12-08 11:52:56 字数 443 浏览 0 评论 0 原文

我如何在 preg_match 正则表达式中转义这样的字符: ' ( ) ,因为我运行此代码时遇到问题:

preg_match("'javascript:window.open('(.*?)')'si", $source, $export);

$source 变量具有以下值: javascript:window.open('http://www.google.com')

并且我想取出 http://www.google.com 并将其放入 $export 中。

how can i escape characters like this: ' ( ) in preg_match regex, because i have problem running this code:

preg_match("'javascript:window.open('(.*?)')'si", $source, $export);

the $source variable have this value: javascript:window.open('http://www.google.com')

and i want to take out http://www.google.com from $source and put it into $export.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风月客 2024-12-15 11:52:56

您可以使用 sscanf$link 字符串>文档:(

$source = "javascript:window.open('http://www.google.com')";

sscanf($source, "javascript:window.open('%[^']", $link);

echo $link;

演示)好处是语法是比正则表达式更容易理解,您可以将值分配给直接变量。

如果您想使用正则表达式,则需要引用特殊字符(preg_quote文档)在创建模式之前。这需要更多的工作,因为您必须在运行之前构建正则表达式模式:

# bare pattern, placeholder for matching group:
$pattern = "javascript:window.open('%s')"; 

# quote the pattern, you use ' as delimiter, it needs to be quoted
$pattern = preg_quote($pattern, "'");

# build full regex with delimiters, modifiers and inserting your match group
$pattern = sprintf("'$pattern'is", '(.*?)');

# run it
preg_match($pattern, $source, $export);

Demo

这将导致以下模式:

'javascript\:window\.open\(\'(.*?)\'\)'is

或作为有效的 PHP 字符串:

$pattern = '\'javascript\\:window\\.open\\(\\\'(.*?)\\\'\\)\'is';

或您的示例:

preg_match('\'javascript\\:window\\.open\\(\\\'(.*?)\\\'\\)\'is', $source, $export);

You can just extract your $link string using sscanfDocs:

$source = "javascript:window.open('http://www.google.com')";

sscanf($source, "javascript:window.open('%[^']", $link);

echo $link;

(Demo) The benefit is that the syntax is easier to understand than with regular expressions and you can assign values to variables directly.

In case you want to use regular expressions, you need to quote special characters (preg_quoteDocs) before you create your pattern. This needs more work, as you must build the regex pattern prior running it:

# bare pattern, placeholder for matching group:
$pattern = "javascript:window.open('%s')"; 

# quote the pattern, you use ' as delimiter, it needs to be quoted
$pattern = preg_quote($pattern, "'");

# build full regex with delimiters, modifiers and inserting your match group
$pattern = sprintf("'$pattern'is", '(.*?)');

# run it
preg_match($pattern, $source, $export);

Demo

This will result in the following pattern:

'javascript\:window\.open\(\'(.*?)\'\)'is

Or as a valid PHP string:

$pattern = '\'javascript\\:window\\.open\\(\\\'(.*?)\\\'\\)\'is';

or your example:

preg_match('\'javascript\\:window\\.open\\(\\\'(.*?)\\\'\\)\'is', $source, $export);
攀登最高峰 2024-12-15 11:52:56

您始终可以使用反斜杠 (\) 转义字符。在你的情况下:

preg_match("'javascript:window.open\(\'(.*?)\'\)'si", $source, $export);

You can always escape characters with the backslash (\). In your case:

preg_match("'javascript:window.open\(\'(.*?)\'\)'si", $source, $export);
影子的影子 2024-12-15 11:52:56

您必须通过 \ 来排除任何特殊字符。请尝试这个:

$source = "javascript:window.open('http://www.google.com')";
preg_match("/javascript:window.open\\('(.*?)'\\)/si", $source, $export);
print_r($export);

You have to excape any special character by \. Please try this:

$source = "javascript:window.open('http://www.google.com')";
preg_match("/javascript:window.open\\('(.*?)'\\)/si", $source, $export);
print_r($export);
我的黑色迷你裙 2024-12-15 11:52:56

使用此字符在您要使用的字符之前进行转义
"\"

如果字符串中有字符 (.,:,(,),\,/,-,[,],#,...) 并且您希望它们与字符串匹配你需要将它们放在模式中 scape by "\"

 (\.,\:,\(,\),\\\\,\/,\-,\[,\],\#,...)

Use this character to scape before the character you want to use
"\"

If there are characters (.,:,(,),\,/,-,[,],#,...) in your string and you want them to match the string you need to have them in the pattern scape by "\"

 (\.,\:,\(,\),\\\\,\/,\-,\[,\],\#,...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文