替换模式以从数据 php 中提取图像 url
replace a pattern to extract an image url from data php
我正在尝试弄清楚如何从这个模式中提取 url
![enter image description here][1] [1]: http://www.codefixup.com/wp-content/uploads/2016/09/pattern-matching-in-php.png
我只需要 http 部分,这样我就可以将它放在图像标签中。图片描述可能会改变,我该如何使用正则表达式或 preg_replace?
假设您想要捕获整个 url 以下内容应该有效
(?:\: )(?P<URL>.*)
示例:https://regex101.com/r/aV1fJ7/1
请注意,如果您的描述类似于 "Check out this cool dog: "
,这将不起作用
或者类似的东西。
我可以通过做...
(?:\[\d+]\: )(?P<URL>.*)
这个与更具体的工作的实例:https://regex101.com/r/aV1fJ7/2
从 img src="" 标签中提取的示例 3
(?:src=\")(?P<URL>.*)\"
https://regex101.com/r/aV1fJ7/3
还有一些示例代码展示了如何捕获、转换和输出图像标签:
<?php
$urlstring ='![enter image description here][1] [1]: http://www.codefixup.com/wp-content/uploads/2016/09/pattern-matching-in-php.png';
$regex = '/ (?:\[\d+\]\: )(?P<URL>.*)/';
//echo $regex.'--'.$urlstring;
if (preg_match($regex, $urlstring, $matches)) {
echo "img src=\"".$matches[1]."\"";
} else {
echo "The regex pattern does not match. :(";
}
?>
我正在尝试弄清楚如何从这个模式中提取 url
![enter image description here][1] [1]: http://www.codefixup.com/wp-content/uploads/2016/09/pattern-matching-in-php.png
我只需要 http 部分,这样我就可以将它放在图像标签中。图片描述可能会改变,我该如何使用正则表达式或 preg_replace?
假设您想要捕获整个 url 以下内容应该有效
(?:\: )(?P<URL>.*)
示例:https://regex101.com/r/aV1fJ7/1
请注意,如果您的描述类似于 "Check out this cool dog: "
,这将不起作用或者类似的东西。
我可以通过做...
(?:\[\d+]\: )(?P<URL>.*)
这个与更具体的工作的实例:https://regex101.com/r/aV1fJ7/2
从 img src="" 标签中提取的示例 3
(?:src=\")(?P<URL>.*)\"
https://regex101.com/r/aV1fJ7/3
还有一些示例代码展示了如何捕获、转换和输出图像标签:
<?php
$urlstring ='![enter image description here][1] [1]: http://www.codefixup.com/wp-content/uploads/2016/09/pattern-matching-in-php.png';
$regex = '/ (?:\[\d+\]\: )(?P<URL>.*)/';
//echo $regex.'--'.$urlstring;
if (preg_match($regex, $urlstring, $matches)) {
echo "img src=\"".$matches[1]."\"";
} else {
echo "The regex pattern does not match. :(";
}
?>