简体   繁体   中英

preg_match_all pattern html tag issue

There are 4 different img tag

<img src="img1.jpg" alt="test" />
<img src="img2.jpg" height="21"/>
<img src="img3.jpg" border="1">
<img src="img4.jpg" >

I use PHP code

preg_match_all('#<img (.*?)([/>| />|>| >])#si',$this->Data,$img);  

but This PHP code result

Array
  (
[1] => Array
    (
        [0] => src="img1.jpg"
        [1] => src="img2.jpg"
        [2] => src="img3.jpg"
        [3] => src="img4.jpg"

    )  

I want to result :

    Array
  (
[1] => Array
    (
        [0] => src="img1.jpg" alt="test"
        [1] => src="img2.jpg" height="21"
        [2] => src="img3.jpg" border="1"
        [3] => src="img4.jpg"

    )  

Can you help me pls ?

try this:

preg_match_all('#<img (.*?)\s?/?>#is', $this->Data, $img);  

the \\s? optionally matches white space sollowed by a optional slash /? .

this will essentially get any content from a well formed img tag. (assuming you don't have any > in the values for the attributes).

as something to keep in mind, regex is not the correct tool for parsing html - but for small - limited - matches it is very useful.

That looks like giving what you want;

$s = '<img src="img1.jpg" alt="test" />
      <img src="img2.jpg" height="21"/>
      <img src="img3.jpg" border="1">
      <img src="img4.jpg" >';
preg_match_all('~<img\s+(.+?)([\s/>]|)>~i', $s, $m);
print_r($m);

Out;

Array
(
    ...
    [1] => Array
        (
            [0] => src="img1.jpg" alt="test" 
            [1] => src="img2.jpg" height="21"
            [2] => src="img3.jpg" border="1"
            [3] => src="img4.jpg" 
        )

By thet way, as far as I know, you don't need to use s flag for preg_match_all , cos of it's matching *_all .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM