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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get Image Alt Attribute value (ALT="episode 48 ")

Status
Not open for further replies.

Leopard2014

Programmer
Oct 30, 2012
4
0
0
NL
hi all i got the image html in $matches. I wonder how i can only get the value of ALT= which in this case would be episode 48?

$matches="<IMG SRC=" BORDER=0 ALT="episode 48" style="cursor:hand"

i dont know how to use preg_match to copy alt value only! could you guys show me how ? Thanks in advance.
 
firs of all your $matches is wrong. but preg_match should look like ( I didn`t test it, written here by hand )
PHP:
$matches="<IMG SRC="[URL unfurl="true"]http://images.somesites.com/videos/3.jpg"[/URL] BORDER=0 ALT="episode 48" style="cursor:hand"
preg_match(‘/<IMG SRC="[URL unfurl="true"]http://images.somesites.com/videos/3.jpg"[/URL] BORDER=0 ALT="([^>]*)”  style="cursor:hand">/’, $matches, $founded)
 
Hi

[ul]
[li]First of all, let us use at least a syntactically valid assignment for the $matches variable. Its value should be a string. You know, enclosed in either single quotes ( ' ) or double quotes ( " ) or here-document. But any syntax you use, the delimiters must be balanced. Failing that you make clear that you not even tried to solve the problem.[/li]
[li]Then you can continue with Krystian81's suggestion, but with small changes :[ul]
[li]Enclose the string in straight quotes, not fancy typographic characters.[/li]
[li]Either choose a delimiter for the regular expression which is not part of the expression, or escape its occurrences inside the expression.[/li]
[li]Include as few static text as possible in the expression to match more various cases.[/li][/ul][/li]
[/ul]
PHP:
[navy]$matches[/navy][teal]=[/teal][green][i]'"<IMG SRC="[URL unfurl="true"]http://images.somesites.com/videos/3.jpg"[/URL] BORDER=0 ALT="episode 48" style="cursor:hand"'[/i][/green][teal];[/teal]
[COLOR=darkgoldenrod]preg_match[/color][teal]([/teal][green][i]"/<img\\b[^>]+\\balt=([\"'])(.*?)\\1/i"[/i][/green][teal],[/teal] [navy]$matches[/navy][teal],[/teal] [navy]$found[/navy][teal]);[/teal]
On successful match the alt value will be in $found[2].

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top