Re: How to find <img> tag and get src of image
| From: | Philip Thompson | Date: | Mon, 24 Apr 2006 13:29:27 +0000 |
| Subject: | Re: How to find <img> tag and get src of image | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-234619@lists.php.net to get a copy of this message | ||
On Apr 23, 2006, at 10:10 PM, Paul Novitski wrote:
At 05:30 PM 4/23/2006, Pham Huu Le Quoc Phuc wrote:Paul, I think you are too kind. I think my response would have been likes Jay's... RTFM. However, you went above and beyond. Isn't this listserv great!! =D ~PhilI want to get src of image form a $text, I have a below text $Text = <table border="1" cellpadding="3%" cellspacing="3%" width="100%">Pham, Before you do anything else, fix your HTML syntax which is full of errors. Your </table> tag appears in the middle of the table and your id syntax is invalid:<tr> <td width="20%"> <img align="middle" border="0" id=userupload/78/Autumn.jpgsrc=userupload/78/Autumn.jpg onClick="javascript:ViewImage(this.id);" width="100" height="100" ></td><td></td></tr> </table> </td> <td class="frame2_middle_right"></td> </tr> <tr> <td class="frame2_bottom_left"></td><td class="frame2_bottom_center"> </td>id=userupload/78/Autumn.jpgThe specification for id [1] says it's an element of type 'name' [2]: <quote> ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). </quote> In other words, you can't have slashes in your id. Put your HTML through the W3C markup validator before trying to write script that parses it: http://validator.w3.org/ When you're ready, you can locate an image src using a Regular Expression such as this:/<img\s+[^>]*src=\"([^"])*[^>]*>\"/which means:<img\s+ = <img followed by one or more whitespace characters[^>]* = zero or more characters not including a close-bracketsrc=\" = src="([^"])* = zero or more characters not including a close-quote (THIS IS YOUR SRC VALUE)\" = close-quote[^>]* = any number of characters excluding close-bracket= close-bracket The src attribute value will be captured by $aMatches[1] by the logic:$sRegExp = <<< hdRE /<img\s+[^>]*src=\"([^"])*[^>]*>\"/ hdRE;preg_match($sRegExp, $sText, $aMatches);This RegExp is designed to confine its result to a single HTML IMG tag. Documentation on PHP regular expression processing begins here: http://php.net/pcre Regards, Paul [1] specification for 'id': http://www.w3.org/TR/html4/struct/global.html#adef-id [2] element type 'name': http://www.w3.org/TR/html4/types.html#type-name