pirpirim
pirpirim

Reputation: 93

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 ?

Upvotes: 0

Views: 1334

Answers (2)

Kerem
Kerem

Reputation: 11566

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.

Upvotes: 0

Eldarni
Eldarni

Reputation: 425

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.

Upvotes: 1

Related Questions