casbboy Posted September 27, 2006 Share Posted September 27, 2006 I have a table with rows of small bits of html code.I have already set it up to look for each row that has an image in the code like so<img src="http://www.site.com/file.jpg" width="80" height="90">I need to create a program with preg_match and preg_replace that will first find the image file's source and then remove the entire image tag.the image tags can be slightly different. some have the '/' at the end and some come with alt tags. How can I do this? I need to get the source then remove the whole thing entirely.ThanksRyan Link to comment https://forums.phpfreaks.com/topic/22302-preg_match-image-source-than-remove-entire-image-tag-from-html/ Share on other sites More sharing options...
effigy Posted September 27, 2006 Share Posted September 27, 2006 [code]<pre><?php $tests = array( '<img>', '<img src="1.jpg">', '<img src="1.gif"/>', '<img src="abc.png" />', '<img alt="image" src="1a.gif">', 'abc<img src="1.jpg">123', 'abc<img src="1.jpg">123<img alt="image" src="1a.gif" />xyz', ); $sources = array(); foreach ($tests as $test) { echo 'Original: ' . htmlentities($test), '<br/>'; while (preg_match('/<img(.*?)>/', $test, $matches)) { if (preg_match('/src="(.+?)"/', $matches[1], $source_matches)) { array_push($sources, $source_matches[1]); echo 'Source: ', $source_matches[1], '<br/>'; } $test = preg_replace('/' . preg_quote($matches[0], '/') . '/', '', $test); } echo 'After: ' . htmlentities($test), '<br/><br/>'; } echo 'Sources: ', '<br/>'; print_r($sources);?></pre>[/code][quote]Original: <img>After: Original: <img src="1.jpg">Source: 1.jpgAfter: Original: <img src="1.gif"/>Source: 1.gifAfter: Original: <img src="abc.png" />Source: abc.pngAfter: Original: <img alt="image" src="1a.gif">Source: 1a.gifAfter: Original: abc<img src="1.jpg">123Source: 1.jpgAfter: abc123Original: abc<img src="1.jpg">123<img alt="image" src="1a.gif" />xyzSource: 1.jpgSource: 1a.gifAfter: abc123xyzSources: Array( [0] => 1.jpg [1] => 1.gif [2] => abc.png [3] => 1a.gif [4] => 1.jpg [5] => 1.jpg [6] => 1a.gif)[/quote] Link to comment https://forums.phpfreaks.com/topic/22302-preg_match-image-source-than-remove-entire-image-tag-from-html/#findComment-99918 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.