ezpickinz Posted April 20, 2013 Share Posted April 20, 2013 I have an image name (pic01.jpg) and the directory it lives in on the remote server ( I also have permission to be there). I know that in many cases there will be a small stack of images that together make a set like, pic01.jpg, pic01_blue.jpg, pic01_yk7z.jpg, pic01_8m.jpg... I know I can use curl to get a response code but then I'd have to know the name of the file first. I believe this will be a small job using Regex. Trouble is every time I begin to read about Regex my eyes glaze over so I'm really not sure. Your help is greatly appreciated. John Quote Link to comment Share on other sites More sharing options...
ezpickinz Posted April 20, 2013 Author Share Posted April 20, 2013 ...Also, there is no index for the image dir so it can be read as a page. I think I may have just answered my own Q. Please don't stop thinking for me though. If you have a solution I'd love to hear it... Quote Link to comment Share on other sites More sharing options...
ezpickinz Posted April 20, 2013 Author Share Posted April 20, 2013 Well here's what I have so far. This code does function! It still needs some work but I've no work left in me this evening. I'm testing on my local it will work on a remote server. <?php function get_url_contents($url){ $crl = curl_init(); $timeout = 5; curl_setopt ($crl, CURLOPT_URL,$url); curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); $ret = curl_exec($crl); curl_close($crl); return $ret; } $imagename = "VTKBC55"; $namesplit = "_"; $extension = ".jpg"; $imgsArray = array(); $image_dir = 'localhost/phpTest/images/'; $pageHTML = get_url_contents($image_dir); $doc = new DOMDocument(); @$doc->loadHTML($pageHTML); $imgTags = $doc->getElementsByTagName('a'); foreach ($imgTags as $img) { $img = pathinfo($img->getAttribute('href')); $imgsArray[] = $img['filename']; echo "FILES: " . $img['filename'] . "<br />"; } foreach($imgsArray as $img){ if(preg_match('/'.$imagename.$namesplit.'*?/',$img,$matches)){ echo "Matched: " . $img . '<br />'; print_r($matches); echo "<br />"; } } ?> Quote Link to comment Share on other sites More sharing options...
ezpickinz Posted April 20, 2013 Author Share Posted April 20, 2013 So here's where I'm at now. The trouble is that it will match anything after filename_ even when the extension changes. How can I say: preg_match('/^'.$imagename.$namesplit. ' *? $img[extension] /' ,$this_img_name,$matches) ???? foreach ($imgTags as $img) { $img = pathinfo($img->getAttribute('href')); $imgsArray[] = array('filename'=>$img['filename'],'extension'=>$img['extension']); //echo "FILES: " . $img['filename'] . "<br />"; } print_r($imgsArray); echo "<br /><br /><br /><br /><br />"; foreach($imgsArray as $img){ $this_img_name = $img['filename']. $img['extension']; if(preg_match('/^'.$imagename.$namesplit.'/',$this_img_name,$matches)){ echo "Matched: " . $img['filename'] . '.' . $img['extension'] . '<br />'; print_r($matches); echo "<br />"; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.