Need Hint/suggestion To Achive Result

Discussion in 'PHP' started by ironmankho, Feb 14, 2013.

  1. #1
    Hi every one

    i have 100 pages that consist on text(html) ......some have single jpg and some have 2 jpg ....... now i want build a checker that will differentiate these pages and separate single jpg and 2 jpg page(i will write myself a code who will move those pages) but my problem how or what logic behind to tell me this page have single jpg and another have 2 jpg.
    (i have good idea about reading text file,if else condition , loop)
    looking for wise advice, suggestion
    Thanks
     
    ironmankho, Feb 14, 2013 IP
  2. Sano000

    Sano000 Active Member

    Messages:
    52
    Likes Received:
    4
    Best Answers:
    5
    Trophy Points:
    53
    #2
    I did not understand what the problem is. But if you have problem with file parsing, you can use http://simplehtmldom.sourceforge.net/manual.htm
    For example:
    
    <?php
    include_once 'simple_html_dom.php';
     
    print_r(img_count('filename'));
     
    // return img tag count in file (for example)
    function img_count($filename) {
        $html = file_get_html($filename);
        return count($html->find('img'));  //
    }
    ?>
    
    PHP:
     
    Sano000, Feb 14, 2013 IP
  3. eldiablo

    eldiablo Active Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Or use simply preg_match to find .jpg files and if count is more then do something if count is 1 or if count is 2...? If you have design related .jpg files and there are 10 of them for example, simply set script to search if there are 11 files or 12 files and then do your magic... :)
     
    eldiablo, Feb 14, 2013 IP
  4. Sano000

    Sano000 Active Member

    Messages:
    52
    Likes Received:
    4
    Best Answers:
    5
    Trophy Points:
    53
    #4
    preg_match_all you mean? :)
     
    Sano000, Feb 14, 2013 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    regex can be difficult -- but you don't need no stupid bloated library like simplehtmldom when PHP already has a perfectly good DOM parser!

    
    function imageCount($filename) {
    	$dom = new DOMDocument();
    	$dom->loadHTMLFile($filename);
    	return $dom->getElementsByTagName('img')->length;
    }
    Code (markup):
    Though that returns all img tags, if you want to differentiate jpeg's separate from any other img...

    
    function jpegCount($filename) {
    	$dom = new DOMDocument();
    	$dom->loadHTMLFile($filename);
    	$count=0;
    	if ($list=$dom->getElementsByTagName('img')) {
    		foreach ($list as $img) {
    			$test = strtolower(substr($img->getAttribute('src'),-4))
    			if ( ($test == '.jpg') || ($test == 'jpeg')) $count++;
    		}
    	}
    	return $count;
    }
    Code (markup):
    Not that hard. (assuming I didn't make any typo's since I'm on the laptop, so code untested)
     
    deathshadow, Feb 14, 2013 IP