Re: How to find <img> tag and get src of image

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 05:30 PM 4/23/2006, Pham Huu Le Quoc Phuc wrote:
I want to get src of image  form a $text, I have a below text

$Text = <table border="1" cellpadding="3%" cellspacing="3%" width="100%">
   <tr>    <td width="20%">
     <img align="middle" border="0" id=userupload/78/Autumn.jpg
src=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>


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:

        id=userupload/78/Autumn.jpg

The 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-bracket

        src=\" = 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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux