如何将javascript正则表达式转换为php正则表达式

如何将javascript正则表达式转换为php正则表达式,javascript,php,regex,Javascript,Php,Regex,我想把这个javascript正则表达式转换成php正则表达式。我已经测试了代码是否可以运行 <img.*?src='(?!http[s]*:\/\/img.[a-zA-Z0-9]*.test\/)(.*?)' <img alt='' src='http://img.dev.teeeest/images/UID' /> <img alt='' src='https://img.deaaav.test.com/images/UID/' /> <img alt=

我想把这个javascript正则表达式转换成php正则表达式。我已经测试了代码是否可以运行

<img.*?src='(?!http[s]*:\/\/img.[a-zA-Z0-9]*.test\/)(.*?)'

<img alt='' src='http://img.dev.teeeest/images/UID' />
<img alt='' src='https://img.deaaav.test.com/images/UID/' />
<img alt='' src='http://api.com/images/UID' />
<img alt='' src='http://img.deaSassav.test/images/UID' />
<img alt='' src='https://img.dev.test/images/UID' />
<img\b.*?\bsrc=['"](?!https?:\/\/img\.[a-z0-9]*\.test\/)([^'"]*)

您的javascript正则表达式工作正常;您只需将
s
标志更改为
m

<pre>Array
(
    [0] => <img alt='' src='http://img.dev.teeeest/images/UID' />
    [1] => <img alt='' src='https://img.deaaav.test.com/images/UID.ux/' />
    [2] => <img alt='' src='http://api.com/images/UID' />
)
</pre>
$body=”
";
$body=stripslashes($body);
$img_array=array();
预匹配所有(“/”;
打印($img_数组[0]);
回声“;
输出:

数组
(
[0] => 
[1] => 
[2] => 
)

此表达式可能也适用:



如果您希望探索/简化/修改该表达式,它已被删除 在的右上面板上进行了说明 .如果你愿意,你可以 也可以观看,它将如何匹配 对照一些样本输入



我只是不知道如何编写它…我尝试了很多函数。但是没有相同的结果。你可以包含你说你尝试过的函数,但没有得到正确的结果吗?这是我关于stackoverflow的第一个问题。可能有一些错误的表达。很抱歉,很清楚你想要做什么,但是,很难帮助你如果您不包括您的尝试。]*?>这是我的php正则表达式。它可以选择最后两个。但我想要另一个。它对php不起作用,但感谢您的回答。@晚上我给了您一个php工作演示。什么意思,它不起作用?@Night如果您想要与另一个答案完全相同的输出,只需更改
$img_array[0]
$img\u数组[1]
是的……我刚试过。它在$img\u数组[1]上。谢谢
http[s]*:\/\/img.[a-zA-Z0-9]*.test\/
$body = "<img alt='' src='http://img.dev.teeeest/images/UID' />
<img alt='' src='https://img.deaaav.test.com/images/UID.ux/' />
<img alt='' src='http://api.com/images/UID' />
<img alt='' src='http://img.deaSassav.test/images/UID' />
<img alt='' src='https://img.dev.test/images/UID' />";
$body = stripslashes($body);
$img_array = array();
preg_match_all("/<img.*?src='(?!http[s]*:\/\/img.[a-zA-Z0-9]*.test\/)(.*?)/imU", $body, $img_array);
echo "<pre>";
print_r($img_array[0]);
echo "</pre>";
<pre>Array
(
    [0] => <img alt='' src='http://img.dev.teeeest/images/UID' />
    [1] => <img alt='' src='https://img.deaaav.test.com/images/UID.ux/' />
    [2] => <img alt='' src='http://api.com/images/UID' />
)
</pre>
<img\b.*?\bsrc=['"](?!https?:\/\/img\.[a-z0-9]*\.test\/)([^'"]*)