SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: difficult ?

  1. #1
    SitePoint Member
    Join Date
    Jul 2003
    Location
    congleton
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    difficult ?

    Hi all

    I need to open a file and loop through it and find all the image file names it contains.

    The file is a html file so i need to loop through the code and find all occurances of <img> and then extract just the file name in the src attribute for each img in the document.

    Can anyone show me how to do this using regular extressions or any other way???

    Best Regards
    Richard Moss

  2. #2
    SitePoint Addict Php_penguin's Avatar
    Join Date
    Aug 2004
    Location
    Colwyn Bay, Wales, UK
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if this file is a valid XHTML file then you could feasibly open it up using an XML parsing function.

    Another way would be to use the wonderful jQuery (javascript) to select all the img tags and send the src(s) off to a PHP script, via AJAX.

    using regex is quite complex, and so you need to be hopeful some freaky guru notices this topic and gives you some meaningless gibberish which will help you out.
    Try changing the topic name to "regex help" or similiar.

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Quick 'n' dirty function:

    Code PHP:
    <?php
     
    	function findImages($text) {
     
    		$text_to_search = file_get_contents($text);
     
    		preg_match_all('/\<img src="(.*)"\>/', $text_to_search, $matches);
     
    		print_r($matches);
     
    	}
     
     
    	findImages("sp_findImages_text.htm");
     
     
    ?>

    based on a simple HTM file:
    Code HTML4Strict:
    <img src="myimage.jpg">
    <p>asda</p>
    <img src="myimage2.jpg">
    <p>kjashd kakhlajsh </p>

    returns an array of values:

    Code:
    Array ( [0] => Array ( [0] =>[*NOTE*]  [1] =>[*NOTE*]  ) [1] => Array ( [0] => myimage.jpg [1] => myimage2.jpg ) )
    *NOTE* attempts to show the image.

    HTH

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by Php_penguin View Post
    using regex is quite complex, and so you need to be hopeful some freaky guru notices this topic and gives you some meaningless gibberish which will help you out.
    Spike, is he talking about you?
    Saul

  5. #5
    SitePoint Evangelist
    Join Date
    Apr 2006
    Location
    Halifax, Canada
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But what if the image has a class, style, alt text, or any other attribute as most images would? That takes a more complicated expression. Here is what I came up with, it works but there might be an easier way to do it.

    PHP Code:
    preg_match_all('/<img (?:[^>"]+|"[^"]*")*?src\\s*=\\s*"?([^\\s]+?)"?(?:[^>"]+|"[^"]*")*?>/i'$subject$matchPREG_PATTERN_ORDER);
    print_r($match); 
    (Assuming that the HTML content to be parsed is in the $subject variable)

    Off Topic:


    / I'm not a freaky guru
    // I swear
    Paul Butler.org
    JSSpamBlock - Reduce WordPress spam.

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by php_daemon View Post
    Off Topic:



    Spike, is he talking about you?
    apparently so..
    Quote Originally Posted by paulgb View Post
    Off Topic:


    / I'm not a freaky guru
    // I swear
    oh you so are!
    the fact that you commented the ot with // proves it beyond doubt....
    (except the first line would throw up a parse error. !)
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Addict Php_penguin's Avatar
    Join Date
    Aug 2004
    Location
    Colwyn Bay, Wales, UK
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no, dont you know the "off topic" is more bullet proof than a CDATA section?

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •