I'm attempting to run preg_match to extract the SRC attribute from the first IMG tag in an article (in this case, stored in $row->introtext).
我尝试运行preg_match从文章中的第一个IMG标记中提取SRC属性(在本例中,存储在$row->introtext中)。
preg_match('/\< *[img][^\>]*[src] *= *[\"\']{0,1}([^\"\']*)/i', $row->introtext, $matches);
Instead of getting something like
而不是得到类似的东西
images/stories/otakuzoku1.jpg
from
从
<img src="images/stories/otakuzoku1.jpg" border="0" alt="Inside Otakuzoku's store" />
I get just
我只得到
0
The regex should be right, but I can't tell why it appears to be matching the border attribute and not the src attribute.
regex应该是正确的,但是我无法说明为什么它看起来与border属性匹配,而不是与src属性匹配。
Alternatively, if you've had the patience to read this far without skipping straight to the reply field and typing 'use a HTML/XML parser', can a good tutorial for one be recommended as I'm having trouble finding one at all that's applicable to PHP 4.
另外,如果您有耐心阅读本文,而不直接跳到reply字段并输入“使用HTML/XML解析器”,那么可以推荐一篇好的教程,因为我很难找到适用于PHP 4的教程。
PHP 4.4.7
PHP 4.4.7
6 个解决方案
#1
31
Your expression is incorrect. Try:
你的表达是不正确的。试一试:
preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $row->introtext, $matches);
Note the removal of brackets around img and src and some other cleanups.
注意img和src周围的支架和其他一些清理。
#2
5
Here's a way to do it with built-in functions (php >= 4):
使用内置函数(php >= 4)实现此功能的方法如下:
$parser = xml_parser_create();
xml_parse_into_struct($parser, $html, $values);
foreach ($values as $key => $val) {
if ($val['tag'] == 'IMG') {
$first_src = $val['attributes']['SRC'];
break;
}
}
echo $first_src; // images/stories/otakuzoku1.jpg
#3
2
Try:
试一试:
include ("htmlparser.inc"); // from: http://php-html.sourceforge.net/
$html = 'bla <img src="images/stories/otakuzoku1.jpg" border="0" alt="Inside Otakuzoku\'s store" /> noise <img src="das" /> foo';
$parser = new HtmlParser($html);
while($parser->parse()) {
if($parser->iNodeName == 'img') {
echo $parser->iNodeAttributes['src'];
break;
}
}
which will produce:
这将会产生:
images/stories/otakuzoku1.jpg
It should work with PHP 4.x.
它应该与PHP 4.x一起工作。
#4
1
The regex I used was much simpler. My code assumes that the string being passed to it contains exactly one img tag with no other markup:
我使用的regex要简单得多。我的代码假设传递给它的字符串只包含一个img标记,没有其他标记:
$pattern = '/src="([^"]*)"/';
See my answer here for more info: How to extract img src, title and alt from html using php?
更多信息请参见我的答案:如何使用php从html中提取img src、title和alt ?
#5
1
If you need to use preg_match()
itself, try this:
如果您需要使用preg_match()本身,请尝试以下操作:
preg_match('/(?<!_)src=([\'"])?(.*?)\\1/',$content, $matches);
#6
0
Please, try this instructions: http://regexlib.com/Search.aspx?k=img&AspxAutoDetectCookieSupport=1
请尝试以下说明:http://regexlib.com/search.aspx?
#1
31
Your expression is incorrect. Try:
你的表达是不正确的。试一试:
preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $row->introtext, $matches);
Note the removal of brackets around img and src and some other cleanups.
注意img和src周围的支架和其他一些清理。
#2
5
Here's a way to do it with built-in functions (php >= 4):
使用内置函数(php >= 4)实现此功能的方法如下:
$parser = xml_parser_create();
xml_parse_into_struct($parser, $html, $values);
foreach ($values as $key => $val) {
if ($val['tag'] == 'IMG') {
$first_src = $val['attributes']['SRC'];
break;
}
}
echo $first_src; // images/stories/otakuzoku1.jpg
#3
2
Try:
试一试:
include ("htmlparser.inc"); // from: http://php-html.sourceforge.net/
$html = 'bla <img src="images/stories/otakuzoku1.jpg" border="0" alt="Inside Otakuzoku\'s store" /> noise <img src="das" /> foo';
$parser = new HtmlParser($html);
while($parser->parse()) {
if($parser->iNodeName == 'img') {
echo $parser->iNodeAttributes['src'];
break;
}
}
which will produce:
这将会产生:
images/stories/otakuzoku1.jpg
It should work with PHP 4.x.
它应该与PHP 4.x一起工作。
#4
1
The regex I used was much simpler. My code assumes that the string being passed to it contains exactly one img tag with no other markup:
我使用的regex要简单得多。我的代码假设传递给它的字符串只包含一个img标记,没有其他标记:
$pattern = '/src="([^"]*)"/';
See my answer here for more info: How to extract img src, title and alt from html using php?
更多信息请参见我的答案:如何使用php从html中提取img src、title和alt ?
#5
1
If you need to use preg_match()
itself, try this:
如果您需要使用preg_match()本身,请尝试以下操作:
preg_match('/(?<!_)src=([\'"])?(.*?)\\1/',$content, $matches);
#6
0
Please, try this instructions: http://regexlib.com/Search.aspx?k=img&AspxAutoDetectCookieSupport=1
请尝试以下说明:http://regexlib.com/search.aspx?