11Tami Posted September 12, 2007 Share Posted September 12, 2007 Hello, how do I add more than one type of image file to preg_grep for example this is working code for looking for a jpg. How to get it to look for a gif as well. $get = preg_grep('/jpg/', $content); I tried variations of this and didn't have any luck looking for jpg or gif. $get = preg_grep('/jpg/||/gif/', $content); Can someone tell me the correct way to list both? Thanks very much. Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 12, 2007 Share Posted September 12, 2007 Try this <?php $ext=array('jpg','gif'); foreach($ext as $val){ $get = preg_grep($val, $content); }else{ echo"sorry wrong file ext"; } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 12, 2007 Share Posted September 12, 2007 Wouldnt this be better. <?php $content='jpg'; $ext=array("jpg","gif"); foreach($ext as $val){ if($content==$val){ echo "correct"; exit; }else{ echo "incorrect"; exit; } } ?> Quote Link to comment Share on other sites More sharing options...
btherl Posted September 12, 2007 Share Posted September 12, 2007 Correct regexp is: preg_grep('/(?:jpg)|(?:gif)/', $content) If you want to match only at the end of each string, use: preg_grep('/(?:jpg)|(?:gif)$/', $content) That will not match "gif.html", but will match "html.gif". That makes sense if your content is actual filenames, but not if it's html. Quote Link to comment Share on other sites More sharing options...
11Tami Posted September 12, 2007 Author Share Posted September 12, 2007 That was it btherl thanks! I'll send you something. Thanks everybody for helping too. Quote Link to comment Share on other sites More sharing options...
effigy Posted September 12, 2007 Share Posted September 12, 2007 If you want to match only at the end of each string, use: preg_grep('/(?:jpg)|(?:gif)$/', $content) This is incorrect. It will require "gif" to match at the end, but not "jpg." Use /(?:jpg|gif)$/. /(?:jpe?g|gif)$/ may even be better. Quote Link to comment Share on other sites More sharing options...
11Tami Posted September 12, 2007 Author Share Posted September 12, 2007 Thanks very much it doesn't need to match at the end, but still have a problem. If it finds an image it shows the image, but if there is no image it is supposed to pull the file http://www.website.com/textfile.txt at a real web site address. But its not, its only pulling the images not the file http://www.website.com/textfile.txt . Can someone please tell me what's wrong with my else? Thanks. <?php ini_set('error_reporting', 8191); ini_set('display_startup_errors', 1); ini_set('display_errors', 1); $content = Array(); $content[1] = "http://www.website.com/textfile.txt"; $content[2] = "<img src='http://www.website.com/thatfile.gif'/>"; $content[3] = "<img src='http://www.website.com/thisfile.jpg'/>"; $contenttimer = date("s")%count($content)+1; if (preg_grep('/(?:jpg)|(?:gif)/', $content)) {$gcontent = $content[$contenttimer]; echo $gcontent;} else { $gcontent = file_get_contents($content[$contenttimer]); echo $gcontent;} ?> Quote Link to comment Share on other sites More sharing options...
btherl Posted September 12, 2007 Share Posted September 12, 2007 Thanks for pointing out my regexp mistake! As for why the "else" is not working, try running this code on its own: echo file_get_contents("http://www.website.com/textfile.txt"); That will tell you if the link actually works and can be accessed with file_get_contents() in the first place. Quote Link to comment Share on other sites More sharing options...
11Tami Posted September 13, 2007 Author Share Posted September 13, 2007 Thanks, yes it works fine like that and with file_get_contents all by itelf, but if I insert it as an else after preg_grep it no longer works and don't know why. Quote Link to comment Share on other sites More sharing options...
btherl Posted September 13, 2007 Share Posted September 13, 2007 Actually, preg_grep() is the wrong function for what you want to do, because it does not allow an "else". Instead you should use preg_match like this: foreach ($content as $c) { if (preg_match('/(?:jpe?g|gif)$/', $c)) { echo $c; } else { $gcontent = file_get_contents($c); echo $gcontent; } } Quote Link to comment Share on other sites More sharing options...
11Tami Posted September 13, 2007 Author Share Posted September 13, 2007 Thanks very much but are you serious? Isn't there a way that anyone knows of? I was trying to avoid preg_match I hate using it, and loved what preg_grep was doing. Can the other of you coders confirm this? I really don't want to go back to preg_match, please let me know, thanks a lot. Quote Link to comment Share on other sites More sharing options...
btherl Posted September 13, 2007 Share Posted September 13, 2007 If you really want to use preg_grep(): $matches = preg_grep('/(?:jpe?g|gif)$/', $content); $non_matches = preg_grep('/(?:jpe?g|gif)$/', $content, PREG_GREP_INVERT); Quote Link to comment Share on other sites More sharing options...
11Tami Posted September 13, 2007 Author Share Posted September 13, 2007 I really appreciate that, I tried invert before and don't know how to get it to work like it is supposed to. But that helped a little bit. Now its showing both items at the same time, its not picking one or the other. Here's what I have if I put else if on the second one it says "unexpected T else." Anyone know a lot about preg grep and invert or know what I'm doing wrong? Thank you. <?php ini_set('error_reporting', 8191); ini_set('display_startup_errors', 1); ini_set('display_errors', 1); $content = Array(); $content[1] = "http://www.website.com/textfile.txt"; $content[2] = "<img src='http://www.website.com/thatfile.gif'/>"; $content[3] = "<img src='http://www.website.com/thisfile.jpg'/>"; $contenttimer = date("s")%count($content)+1; $matches = preg_grep('/(?:jpg)|(?:gif)/', $content); $non_matches = preg_grep('/(?:jpg)|(?:gif)/', $content, PREG_GREP_INVERT); if($matches); {$gcontent = $content[$contenttimer]; echo $gcontent;} if($non_matches); {$gcontent = file_get_contents($content[$contenttimer]); echo $gcontent;} ?> Quote Link to comment Share on other sites More sharing options...
btherl Posted September 13, 2007 Share Posted September 13, 2007 It's time you got familiar with foreach! $matches = preg_grep('/(?:jpg)|(?:gif)/', $content); $non_matches = preg_grep('/(?:jpg)|(?:gif)/', $content, PREG_GREP_INVERT); foreach ($matches as $image) { print $image; } foreach ($non_matches as $url) { $gcontent = file_get_contents($url); echo $gcontent; } The difference with using preg_match() is that you will get the items in the original order. With preg_grep(), you'll get all the images first then the urls second (or the other way around) Quote Link to comment Share on other sites More sharing options...
11Tami Posted September 14, 2007 Author Share Posted September 14, 2007 I really appreciate your taking the time to help btherl. The problem I was having with preg_match is no one could give me a way to pull a image anywhere out of the line of the array. It only worked if the .jpg etc was close to the end of the line. Maybe they were giving me the wrong information, I couldn't find a way to do it with preg_match. But preg_grep was working. Almost all is working with your last post and its very close now, except for one thing. Its showing all array lines instead of just one. I tried attaching $contenttimer to print $image[$contenttimer]; and a few other places I thought might work and it didn't. Anyone know how to pull one line of the array out at a time but keeping most of the rest? Thanks. Quote Link to comment Share on other sites More sharing options...
btherl Posted September 14, 2007 Share Posted September 14, 2007 Can you explain a bit better what you want? Which line do you want to pull out? Better still, tell us what output you want. preg_grep() and preg_match() work exactly the same way. You might want to try again with preg_match using the regexp that effigy provided. preg_match() checks one string only, and is usually used inside an "if" condition. preg_grep() checks an array of strings, and gives you an array of matching strings. Quote Link to comment Share on other sites More sharing options...
11Tami Posted September 14, 2007 Author Share Posted September 14, 2007 Ok thanks, I'll send it to you. 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.