Regex & PHP -将src属性从img标记中分离出来

时间:2022-11-27 18:36:21

With PHP, how can I isolate the contents of the src attribute from $foo? The end result I'm looking for would give me just "http://example.com/img/image.jpg"

使用PHP,如何将src属性的内容与$foo隔离?我所寻找的最终结果只会给我“http://example.com/img/image.jpg”。

$foo = '<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" width="100" height="100" />';

10 个解决方案

#1


63  

If you don't wish to use regex (or any non-standard PHP components), a reasonable solution using the built-in DOMDocument class would be as follows:

如果您不希望使用regex(或任何非标准PHP组件),那么使用内置DOMDocument类的合理解决方案如下:

<?php
    $doc = new DOMDocument();
    $doc->loadHTML('<img src="http://example.com/img/image.jpg" ... />');
    $imageTags = $doc->getElementsByTagName('img');

    foreach($imageTags as $tag) {
        echo $tag->getAttribute('src');
    }
?>

#2


32  

Code

<?php
    $foo = '<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" width="100" height="100" />';
    $array = array();
    preg_match( '/src="([^"]*)"/i', $foo, $array ) ;
    print_r( $array[1] ) ;

Output

http://example.com/img/image.jpg

#3


6  

// Create DOM from string
$html = str_get_html('<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" width="100" height="100" />');

// echo the src attribute
echo $html->find('img', 0)->src;

http://simplehtmldom.sourceforge.net/

http://simplehtmldom.sourceforge.net/

#4


5  

I got this code:

我有这段代码:

$dom = new DOMDocument();
$dom->loadHTML($img);
echo $dom->getElementsByTagName('img')->item(0)->getAttribute('src');

Assuming there is only one img :P

假设只有一个img:P

#5


3  

I'm extremely late to this, but I have a simple solution not yet mentioned. Load it with simplexml_load_string (if you have simplexml enabled) and then flip it through json_encode and json_decode.

我对此已经很晚了,但是我有一个简单的解决方案,还没有提到。使用simplexml_load_string(如果启用了simplexml)加载它,然后通过json_encode和json_decode对其进行翻转。

$foo = '<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" width="100" height="100" />';

$parsedFoo = json_decode(json_encode(simplexml_load_string($foo)), true);
var_dump($parsedFoo['@attributes']['src']); // output: "http://example.com/img/image.jpg"

$parsedFoo comes through as

parsedFoo来自美元

array(1) {
  ["@attributes"]=>
  array(6) {
    ["class"]=>
    string(12) "foo bar test"
    ["title"]=>
    string(10) "test image"
    ["src"]=>
    string(32) "http://example.com/img/image.jpg"
    ["alt"]=>
    string(10) "test image"
    ["width"]=>
    string(3) "100"
    ["height"]=>
    string(3) "100"
  }
}

I've been using this for parsing XML and HTML for a few months now and it works pretty well. I've had no hiccups yet, though I haven't had to parse a large file with it (I imagine using json_encode and json_decode like that will get slower the larger the input gets). It's convoluted, but it's by far the easiest way to read HTML properties.

几个月来,我一直在使用它来解析XML和HTML,它运行得非常好。我还没有遇到任何问题,尽管我不必用它解析一个大文件(我想象使用json_encode和json_decode这样的方法会随着输入的增加而变慢)。它很复杂,但迄今为止,它是读取HTML属性的最简单方法。

#6


1  

try this pattern:

试试这个模式:

'/< \s* img [^\>]* src \s* = \s* [\""\']? ( [^\""\'\s>]* )/'

#7


1  

preg_match solves this problem nicely.

preg_match很好地解决了这个问题。

See my answer here: How to extract img src, title and alt from html using php?

请参见我的答案:如何使用php从html中提取img src、title和alt ?

#8


0  

Here's what I ended up doing, although I'm not sure about how efficient this is:

这是我最后做的,尽管我不确定这有多有效:

$imgsplit = explode('"',$data);
foreach ($imgsplit as $item) {
    if (strpos($item, 'http') !== FALSE) {
        $image = $item;
        break;
    }
}

#9


0  

You can go around this problem using this function:

你可以用这个函数来解决这个问题:


function getTextBetween($start, $end, $text)
{
 $start_from = strpos($text, $start);
 $start_pos = $start_from + strlen($start);
 $end_pos = strpos($text, $end, $start_pos + 1);
 $subtext = substr($text, $start_pos, $end_pos);
 return $subtext;
}
$foo = '<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" width="100" height="100" />';
$img_src = getTextBetween('src="', '"', $foo);

#10


-1  

lets assume i use

让我们假设我使用

$text ='<img src="blabla.jpg" alt="blabla" />';

in

getTextBetween('src="','"',$text);

the codes will return :

代码将返回:

blabla.jpg" alt="blabla" 

which is wrong, we want the codes to return the text between the attribute value quotes i.e attr = "value".

这是错误的,我们希望代码返回属性值引号i之间的文本。e attr =“价值”。

so

所以

  function getTextBetween($start, $end, $text)
            {
                // explode the start string
                $first_strip= end(explode($start,$text,2));

                // explode the end string
                $final_strip = explode($end,$first_strip)[0];
                return $final_strip;
            }

does the trick!.

的把戏!

Try

试一试

   getTextBetween('src="','"',$text);

will return:

将返回:

blabla.jpg

Thanks all the same , because your solution gave me an insight to the final solution .

还是要谢谢你,因为你的解决方案让我对最终的解决方案有了深入的了解。

#1


63  

If you don't wish to use regex (or any non-standard PHP components), a reasonable solution using the built-in DOMDocument class would be as follows:

如果您不希望使用regex(或任何非标准PHP组件),那么使用内置DOMDocument类的合理解决方案如下:

<?php
    $doc = new DOMDocument();
    $doc->loadHTML('<img src="http://example.com/img/image.jpg" ... />');
    $imageTags = $doc->getElementsByTagName('img');

    foreach($imageTags as $tag) {
        echo $tag->getAttribute('src');
    }
?>

#2


32  

Code

<?php
    $foo = '<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" width="100" height="100" />';
    $array = array();
    preg_match( '/src="([^"]*)"/i', $foo, $array ) ;
    print_r( $array[1] ) ;

Output

http://example.com/img/image.jpg

#3


6  

// Create DOM from string
$html = str_get_html('<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" width="100" height="100" />');

// echo the src attribute
echo $html->find('img', 0)->src;

http://simplehtmldom.sourceforge.net/

http://simplehtmldom.sourceforge.net/

#4


5  

I got this code:

我有这段代码:

$dom = new DOMDocument();
$dom->loadHTML($img);
echo $dom->getElementsByTagName('img')->item(0)->getAttribute('src');

Assuming there is only one img :P

假设只有一个img:P

#5


3  

I'm extremely late to this, but I have a simple solution not yet mentioned. Load it with simplexml_load_string (if you have simplexml enabled) and then flip it through json_encode and json_decode.

我对此已经很晚了,但是我有一个简单的解决方案,还没有提到。使用simplexml_load_string(如果启用了simplexml)加载它,然后通过json_encode和json_decode对其进行翻转。

$foo = '<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" width="100" height="100" />';

$parsedFoo = json_decode(json_encode(simplexml_load_string($foo)), true);
var_dump($parsedFoo['@attributes']['src']); // output: "http://example.com/img/image.jpg"

$parsedFoo comes through as

parsedFoo来自美元

array(1) {
  ["@attributes"]=>
  array(6) {
    ["class"]=>
    string(12) "foo bar test"
    ["title"]=>
    string(10) "test image"
    ["src"]=>
    string(32) "http://example.com/img/image.jpg"
    ["alt"]=>
    string(10) "test image"
    ["width"]=>
    string(3) "100"
    ["height"]=>
    string(3) "100"
  }
}

I've been using this for parsing XML and HTML for a few months now and it works pretty well. I've had no hiccups yet, though I haven't had to parse a large file with it (I imagine using json_encode and json_decode like that will get slower the larger the input gets). It's convoluted, but it's by far the easiest way to read HTML properties.

几个月来,我一直在使用它来解析XML和HTML,它运行得非常好。我还没有遇到任何问题,尽管我不必用它解析一个大文件(我想象使用json_encode和json_decode这样的方法会随着输入的增加而变慢)。它很复杂,但迄今为止,它是读取HTML属性的最简单方法。

#6


1  

try this pattern:

试试这个模式:

'/< \s* img [^\>]* src \s* = \s* [\""\']? ( [^\""\'\s>]* )/'

#7


1  

preg_match solves this problem nicely.

preg_match很好地解决了这个问题。

See my answer here: How to extract img src, title and alt from html using php?

请参见我的答案:如何使用php从html中提取img src、title和alt ?

#8


0  

Here's what I ended up doing, although I'm not sure about how efficient this is:

这是我最后做的,尽管我不确定这有多有效:

$imgsplit = explode('"',$data);
foreach ($imgsplit as $item) {
    if (strpos($item, 'http') !== FALSE) {
        $image = $item;
        break;
    }
}

#9


0  

You can go around this problem using this function:

你可以用这个函数来解决这个问题:


function getTextBetween($start, $end, $text)
{
 $start_from = strpos($text, $start);
 $start_pos = $start_from + strlen($start);
 $end_pos = strpos($text, $end, $start_pos + 1);
 $subtext = substr($text, $start_pos, $end_pos);
 return $subtext;
}
$foo = '<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" width="100" height="100" />';
$img_src = getTextBetween('src="', '"', $foo);

#10


-1  

lets assume i use

让我们假设我使用

$text ='<img src="blabla.jpg" alt="blabla" />';

in

getTextBetween('src="','"',$text);

the codes will return :

代码将返回:

blabla.jpg" alt="blabla" 

which is wrong, we want the codes to return the text between the attribute value quotes i.e attr = "value".

这是错误的,我们希望代码返回属性值引号i之间的文本。e attr =“价值”。

so

所以

  function getTextBetween($start, $end, $text)
            {
                // explode the start string
                $first_strip= end(explode($start,$text,2));

                // explode the end string
                $final_strip = explode($end,$first_strip)[0];
                return $final_strip;
            }

does the trick!.

的把戏!

Try

试一试

   getTextBetween('src="','"',$text);

will return:

将返回:

blabla.jpg

Thanks all the same , because your solution gave me an insight to the final solution .

还是要谢谢你,因为你的解决方案让我对最终的解决方案有了深入的了解。