Jump to content

[SOLVED] preg_grep?


11Tami

Recommended Posts

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;}  
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
  }
}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;} ?>

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.