Jump to content

Retrieve html elements from a string


Nicholas Reed

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/126438-retrieve-html-elements-from-a-string/
Share on other sites

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]);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.