preg_match() <img> tag of a page, sometimes it doesn't strip it!!??

Discussion in 'PHP' started by free-designer, Apr 30, 2010.

  1. #1
    hey guys im haveing a problem with this function

    preg_match_all()

    im useing this pattern "/(<img)\s (src=\"( [a-zA-Z0-9\.;:\/\?&=_|\r|\n] (.*) \.(jpg|png) )\")/isxmU"

    to get all images links with jpg and png extinction

    but it will only get the img tag on this way

    <img src='link' .../>

    but if the <img> tag has an height or width it wont get it

    like that

    <img height='50px' width='50px' src='link' />

    and i guess this problem cuz of my pattern im telling it to get only the src which is after the img tag directly but that not what i want i want to get the src of the img where ever the src were

    thanks for anyhelp :)
     
    free-designer, Apr 30, 2010 IP
  2. bytes

    bytes Peon

    Messages:
    39
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    the code:
    
    $str = 'fdjsadfgjlk s<img src="http://fdgdfgsd.com/fsdfg.gif" width="40"/>
    fas
    da
    sdf
    <img width="40" height=30 src="http://fdgdfgsd.com/fsdfg.png" />
    f<img width="40" height=30 src="http://fdgdfgsd.com/fsdfg.xxx" />
    s<img/>';
    
    $matches = array();
    preg_match_all("/<img[\s\S]*?(.jpg|.png|.gif)+?[\s\S]*?>/im",$str,$matches);
    
    var_dump($matches);
    
    PHP:
    gives this:
    
    array(2) {
      [0]=>
      array(2) {
        [0]=>
        string(53) "<img src="http://fdgdfgsd.com/fsdfg.gif" width="40"/>"
        [1]=>
        string(64) "<img width="40" height=30 src="http://fdgdfgsd.com/fsdfg.png" />
    "
      }
      [1]=>
      array(2) {
        [0]=>
        string(4) ".gif"
        [1]=>
        string(4) ".png"
      }
    }
    
    
    PHP:
     
    bytes, Apr 30, 2010 IP
  3. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    well i guess the result is a little bit complicated, i mean my code only getting the img url

    here is my code

    
    	preg_match_all("/(<img)\s (src=\"(  [a-zA-Z0-9\.;:\/\?&=_|\r|\n]   (.*)   \.(jpg|png)  )\")/isxmU", file_get_contents("http://www.deviantart.com"), $images);
    	$limit = 3;
    	$i=0;
    	foreach($images[3] as $image_url):
        	$valid_files = substr($image_url, -3);
    		$i++;
    		if($i == $limit) break;
    			echo $image_url."<br />";
    	endforeach;
    
    PHP:
    you can try it, it will give you two url's back cuz of the limit

    so it will only get the url of the img like that

    <img src='urlofimage.jpg' />

    but if the img tag like this it will get nothing

    <img height='50px' width='100px' src='link.jpg' />

    it dosn't have to be hight and width it can be anything else like alt or title, things like that

    so i want my pattern to get the src where ever the src were

    thank you :)
     
    free-designer, Apr 30, 2010 IP
  4. Spawny

    Spawny Well-Known Member

    Messages:
    252
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    110
    #4
    try this one

    preg_match_all('#<\s*img [^\>]*src\s*=\s*(["\'])(.*?)\1#im',$data,$matches);
    PHP:
     
    Spawny, Apr 30, 2010 IP
  5. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    sorry it gives me Invalid argument supplied for foreach() :)
     
    free-designer, Apr 30, 2010 IP
  6. zerophean

    zerophean Peon

    Messages:
    91
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    if you confuse using RegExp you can try this site to generate :

    http://txt2re.com/

    its support
    Perl,PHP,Python,Java,Javascript,ColdFusion,C,C++, Ruby, VB, VBScript , J#.net, C#.net, C++.net, VB.net
     
    zerophean, May 1, 2010 IP
  7. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    dude, this is awesome :) thank you so mush
     
    free-designer, May 1, 2010 IP