Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression Help (Yes, I've searched...) 1

Status
Not open for further replies.

kenrbnsn

Technical User
Jun 7, 2002
606
US
I want to parse an IMG tag from HTML that will always be in the same format (it's being generated by another program). The tag will always look like:
Code:
<img src="image.PNG" alt="some descriptive text">
I want to parse this so I can get the src file name and the alt text.

I've been looking at examples and trying them until I have a headache... :)

Can anyone help me out.

Thanks in advance.

Ken
 
This little script:

Code:
<?php

$a = '<img src="image.PNG" alt="some descriptive text">';

list ($src_value, $alt_value) = explode ("\t", preg_replace ('/.*src="([^"]*)" alt="([^"]*)".*/', "$1\t$2", $a));

?>

Sets $src_value and $alt_value to the correct values.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks for the script. It works as advertised. :)

Can you explain the search strings a little.

Ken
 
It basically:
eats everything up to 'src="'
then matches everything that's not a doublequote
then eats '" alt="'
then matches everything that's not a doublequote
then eats everything else.

It replaces what it's eaten and matched (which is the entire string) with:
the first-matched substring
a tab character
the second-matched substring

All this is passed to explode(), which breaks the string into an array on the tab character.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks again.

In my experimenting, I was missing the 'eats everything up to 'src="'' piece...

I've taken your script and put to work in a script I've been writing (on and off) for about 2 weeks...

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top