Finding all pictures in text. Parsing.
Find all img images in text, php regular expression. All images are included in the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/* preg_match_all match the regexp in all the $html string and output everything as an array in $result. "i" option is used to make it case insensitive */ preg_match_all('/<img[^>]+>/i',$html, $result); print_r($result); Array ( [0] => Array ( [0] => <img src="/Content/Img/stackoverflow-logo-250.png" alt="logo link to homepage" width="250" height="70" /> [1] => <img class="vote-up" title="This was helpful (click again to undo)" src="/content/img/vote-arrow-up.png" alt="vote up" /> [2] => <img class="vote-down" title="This was not helpful (click again to undo)" src="/content/img/vote-arrow-down.png" alt="vote down" /> [3] => [4] => <img class="vote-up" title="This was helpful (click again to undo)" src="/content/img/vote-arrow-up.png" alt="vote up" /> [...] ) ) |
To parse all images into alt attributes, title, src you can use another regular expression:
1 2 3 4 5 |
$img = array(); foreach( $result as $img_tag) { preg_match_all('/(alt|title|src)=("[^"]*")/i',$img_tag, $img[$img_tag]); } |
The result of executing a regular expression for an array of images.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
print_r($img); Array ( [<img src="/Content/Img/stackoverflow-logo-250.png" alt="logo link to homepage" width="250" height="70" />] => Array ( [0] => Array ( [0] => src="/Content/Img/stackoverflow-logo-250.png" [1] => alt="logo link to homepage" ) [1] => Array ( [0] => src [1] => alt ) [2] => Array ( [0] => "/Content/Img/stackoverflow-logo-250.png" [1] => "logo link to homepage" ) ) |
And so on …
You can also use the HTML code parser PHP Simple HTML DOM Parser
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.