Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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
PHP使用正则表达式查找子字符串的字符串_Php_Regex_String_Parsing - Fatal编程技术网

PHP使用正则表达式查找子字符串的字符串

PHP使用正则表达式查找子字符串的字符串,php,regex,string,parsing,Php,Regex,String,Parsing,我有一个网页源代码,我想在我的项目中使用。我想在此代码中使用图像链接。所以,我想在PHP中使用正则表达式来访问这个链接 就是这样: img src=”http://imagelinkhere.com“class=”图像“ 只有一行是这样的。 我的逻辑是把字符串 =” 及 “class=”图像“ 人物 我怎样才能用正则表达式做到这一点?非常感谢。尝试使用preg\u match\u all,如下所示: preg_match_all('/img src="([^"]*)"/', $source, $

我有一个网页源代码,我想在我的项目中使用。我想在此代码中使用图像链接。所以,我想在PHP中使用正则表达式来访问这个链接

就是这样:

img src=”http://imagelinkhere.com“class=”图像“

只有一行是这样的。 我的逻辑是把字符串

=”

“class=”图像“

人物


我怎样才能用正则表达式做到这一点?非常感谢。

尝试使用preg\u match\u all,如下所示:

preg_match_all('/img src="([^"]*)"/', $source, $images);

这将把所有图像的URL放在
$images
变量中。正则表达式所做的是在代码中查找所有
img src
位并匹配引号之间的位。

尝试使用preg\u match\u all,如下所示:

preg_match_all('/img src="([^"]*)"/', $source, $images);

这将把所有图像的URL放在
$images
变量中。正则表达式所做的是查找代码中的所有
img src
位,并匹配引号之间的位。

有几种方法:

1.你可以使用 我更喜欢使用简单HTML的Dom解析器

2.您也可以使用preg_match

$foo = '<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" class="image" />';
$array = array();
preg_match( '/src="([^"]*)"/i', $foo, $array ) ;
$foo='';
$array=array();
preg_match('/src=“([^”]*)”/i',$foo,$array);

请参见此

有几种方法可以做到这一点:

1.你可以使用 我更喜欢使用简单HTML的Dom解析器

2.您也可以使用preg_match

$foo = '<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" class="image" />';
$array = array();
preg_match( '/src="([^"]*)"/i', $foo, $array ) ;
$foo='';
$array=array();
preg_match('/src=“([^”]*)”/i',$foo,$array);
请使用查看此

.*="(.*)?" .*
with preg replace只提供第一个正则表达式组(\1)中的url

看起来是这样的

$str='img src="http://imagelinkhere.com" class="image"';
$str=preg_replace('.*="(.*)?" .*','$1',$str);
echo $str;
-->

编辑: 或者按照Baba的建议使用DOM解析器。我会记得,使用正则表达式解析html时,它会让您头疼。

使用

.*="(.*)?" .*
preg_match("/(http://+.*?")/",$text,$matches);
var_dump($matches);
with preg replace只提供第一个正则表达式组(\1)中的url

看起来是这样的

$str='img src="http://imagelinkhere.com" class="image"';
$str=preg_replace('.*="(.*)?" .*','$1',$str);
echo $str;
-->

编辑: 或者按照Baba的建议使用DOM解析器。我会记得正则表达式在解析html时会让你头疼

preg_match("/(http://+.*?")/",$text,$matches);
var_dump($matches);
链接将以$matches为单位

链接将以$matches为单位。


我能听到马蹄声,所以我选择了DOM解析而不是正则表达式

$dom = new DOMDocument();
$dom->loadHTMLFile('path/to/your/file.html');
foreach ($dom->getElementsByTagName('img') as $img)
{
    if ($img->hasAttribute('class') && $img->getAttribute('class') == 'image')
    {
        echo $img->getAttribute('src');
    }
}

这将只回显带有
class=“image”

的img标记的src属性。我可以听到蹄声,因此我使用DOM解析而不是regex

$dom = new DOMDocument();
$dom->loadHTMLFile('path/to/your/file.html');
foreach ($dom->getElementsByTagName('img') as $img)
{
    if ($img->hasAttribute('class') && $img->getAttribute('class') == 'image')
    {
        echo $img->getAttribute('src');
    }
}

这将只回显带有
class=“image”

的img标记的src属性不要使用regex解析html不要使用regex解析html问题是,此代码中有30或40个图像。我想用其中一个,而不是全部。他们的结尾没有“class=”image“,只有一个有,我想用那个。这就是为什么我说介于“和”class=”image之间“字符,并希望使用正则表达式。能否将填充HTML添加到。。这样我就能知道你想要什么。。我仍然相信它可以用DomDocumentSure完成,这就是代码。这就是我想要得到的链接:只需将
if($v->getAttribute(“class”)==“image”)
添加到代码中就行了。非常感谢,这比令人头痛的正则表达式简单。问题是,这段代码中有30或40个图像。我想用其中一个,而不是全部。他们的结尾没有“class=”image“,只有一个有,我想用那个。这就是为什么我说介于“和”class=”image之间“字符,并希望使用正则表达式。能否将填充HTML添加到。。这样我就能知道你想要什么。。我仍然相信它可以用DomDocumentSure完成,这就是代码。这就是我想要得到的链接:只需在代码中添加
if($v->getAttribute(“class”)==“image”)
,非常感谢,这比正则表达式更简单。