Nicholas Reed Posted September 30, 2008 Share Posted September 30, 2008 hello. this is my first time on phpfreaks. [ life story goes here... ] I will be dealing with a big string that contains various html elements with attributes. My ultimate goal is to get src="pictureaddress" out of the <img> elements the big string contains, and then add the contents of the src="" (an image address) to the database. The string might contain any html element/tag, and quite some random characters. for example the big string could be: <a href="http://localhost/wordpress/wp-content/uploads/2008/09/5bwallcoo_com5d_photobook_horikitam1.jpg"><img class="alignnone size-medium wp-image-71" title="5bwallcoo_com5d_photobook_horikitam1" src="http://localhost/wordpress/wp-content/uploads/2008/09/5bwallcoo_com5d_photobook_horikitam1-300x225.jpg" alt="" width="300" height="225" /></a><a href="http://localhost/wordpress/wp-content/uploads/2008/09/normal_maki-horikita-0098-92784.jpg"><img class="alignnone size-medium wp-image-73" title="normal_maki-horikita-0098-92784" src="http://localhost/wordpress/wp-content/uploads/2008/09/normal_maki-horikita-0098-92784-300x225.jpg" alt="" width="300" height="225" /></a> [caption id=attachment_72" align="alignnone" width="265" caption="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz]<a href="http://localhost/wordpress/wp-content/uploads/2008/09/maki.jpg"><img class="size-medium wp-image-72" title="maki" src="http://localhost/wordpress/wp-content/uploads/2008/09/maki-265x300.jpg" alt="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" width="265" height="300" /></a><a href="http://localhost/wordpress/wp-content/uploads/2008/09/4f04e9a0102900_full1.jpg"><img class="alignnone size-medium wp-image-70" title="4f04e9a0102900_full1" src="http://localhost/wordpress/wp-content/uploads/2008/09/4f04e9a0102900_full1-300x225.jpg" alt="" width="300" height="225" /></a> So our goal is to get these bad boys out, using some sort of for loop, and adding each one to the database each time. Thank you very much. Nicholas Reed nicholasreed.net Quote Link to comment https://forums.phpfreaks.com/topic/126438-retrieve-html-elements-from-a-string/ Share on other sites More sharing options...
sasa Posted September 30, 2008 Share Posted September 30, 2008 try <?php $a = '<img class="alignnone size-medium wp-image-71" title="5bwallcoo_com5d_photobook_horikitam1" src="http://localhost/wordpress/wp-content/uploads/2008/09/5bwallcoo_com5d_photobook_horikitam1-300x225.jpg" alt="" width="300" height="225" /><img class="alignnone size-medium wp-image-73" title="normal_maki-horikita-0098-92784" src="http://localhost/wordpress/wp-content/uploads/2008/09/normal_maki-horikita-0098-92784-300x225.jpg" alt="" width="300" height="225" /> [caption id="attachment_72" align="alignnone" width="265" caption="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"]<img class="size-medium wp-image-72" title="maki" src="http://localhost/wordpress/wp-content/uploads/2008/09/maki-265x300.jpg" alt="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" width="265" height="300" /><img class="alignnone size-medium wp-image-70" title="4f04e9a0102900_full1" src="http://localhost/wordpress/wp-content/uploads/2008/09/4f04e9a0102900_full1-300x225.jpg" alt="" width="300" height="225" />'; preg_match_all('|<img [^>]*src="([^"]+)"|is',$a,$out); print_r($out[1]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/126438-retrieve-html-elements-from-a-string/#findComment-653827 Share on other sites More sharing options...
discomatt Posted September 30, 2008 Share Posted September 30, 2008 I'd use a regex like so preg_match_all('/<img [^>]*?src="([^"]++)/i', $subject, $result); print_r( $result[1] ); Quote Link to comment https://forums.phpfreaks.com/topic/126438-retrieve-html-elements-from-a-string/#findComment-653835 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.