Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用PHP从Web服务中的图像标记输出中提取src_Regex_Preg Match - Fatal编程技术网

Regex 使用PHP从Web服务中的图像标记输出中提取src

Regex 使用PHP从Web服务中的图像标记输出中提取src,regex,preg-match,Regex,Preg Match,正如pekka在评论中所说,PHP已经内置了用于提取标签和类似性质的东西的工具。您的字符串将是何时使用PHPDOM的一个很好的示例 也就是说,使用regex,我想您可以匹配src=\“和结束语\”之间的所有内容。做到这一点非常简单: preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i'); 你们可以一起做这样的事情: src=\\"(.*?)\\" 或者,如果您喜欢,您可以使用preg_replace将这两行组合起来: http:/

正如pekka在评论中所说,PHP已经内置了用于提取标签和类似性质的东西的工具。您的字符串将是何时使用PHPDOM的一个很好的示例

也就是说,使用regex,我想您可以匹配
src=\“
和结束语
\”
之间的所有内容。做到这一点非常简单:

preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i');
你们可以一起做这样的事情:

src=\\"(.*?)\\"
或者,如果您喜欢,您可以使用preg_replace将这两行组合起来:

http://localhost/example-local/sites/example/files/styles/thumbnail/public/news-story/gallery-images/Desert.jpg?itok=bqyr-vpK
这基本上只是匹配所有内容,并用它在
(.*)
中找到的内容覆盖整个字符串

下面是一个工作演示:


不要使用正则表达式;在您使用的语言/平台中使用HTML解析器。更可靠。PHP示例:
<?php

// SET OUR DEFAULT STRING
$string = "<img class=\"img-responsive\" src=\"http://localhost/example-local/sites/example/files/styles/thumbnail/public/news-story/gallery-images/Desert.jpg?itok=bqyr-vpK\" width=\"100\" height=\"75\" alt=\"\" /><blockquote class=\"image-field-caption\">\r\n  <p>Desert</p>\n</blockquote>\r\n";

// USE PREG MATCH TO FIND THE STRING AND SAVE IT IN $matches
preg_match('~src=\\"(.*?)\\"~i', $string, $matches);

// PRINT IT OUT
print $matches[1];
http://localhost/example-local/sites/example/files/styles/thumbnail/public/news-story/gallery-images/Desert.jpg?itok=bqyr-vpK
print preg_replace('~.*?src=\\"(.*?)\\".*~is', '$1', $string);