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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.