Max García
Max García

Reputation: 189

Find two different substr in PHP

From:

$str = "This is one string with this picture pic1.jpg and here there's another one pic2.png for example"

I want to use something similar to preg_match_all() but I would like to do it just in one line. I want to have in just one array both: [pic1.jpg] and [pic2.png].

Something like this:

preg_match_all(
'/picture (.*) and|one (.*) for/',
$pat,
$matches,
PREG_PATTERN_ORDER));

I know this code is really bad, but I think you have the idea.

Upvotes: 3

Views: 111

Answers (2)

mickmackusa
mickmackusa

Reputation: 48073

This will match your intended image files with less flexibility (improved accuracy) in the file suffix and more flexibility in the filename.

Pattern: (Demo) /\w+\.(?:gif|png|jpe?g)/i

This will match one or more letters/numbers/underscores, followed by a dot, followed by gif, png, jpg, or jpeg (case-insensitively).

If you require more file suffixes, you can extend the expression with another alternative by adding a pipe (|) then your suffix.

If you need to include more possible characters to the front of the filename, you can create a character class like this [\w@!] which will also allow @ and !. Or you could change \w with \S to match all non-white-characters, but this will slow down your pattern a little.

PHP Code: (Demo)

$str="Match four pictures like this: pic1.jpg, pic2.png, pic3.gif, and pic4.jpeg for example, but not these: document5.pdf and excel7.xls.";
var_export(preg_match_all('/\w+\.(?:gif|png|jpe?g)/i',$str,$out)?$out[0]:'failed');

Output:

array (
  0 => 'pic1.jpg',
  1 => 'pic2.png',
  2 => 'pic3.gif',
  3 => 'pic4.jpeg',
)

You will see that by tightening up the file suffix requirements, you can avoid unintentional matches which are not image files.

Upvotes: 1

slevy1
slevy1

Reputation: 3820

Updated response:

<?php

$str = "This is one string with this picture pic1.jpg and here there's another one pic2.png for example";
$pat = '/\w+\d\.[a-zA-Z]{3}/';

$arrImg = null;
if (preg_match_all( $pat, $str, $matches, PREG_PATTERN_ORDER )) {
    $arrImg = array_pop($matches);
    print_r($arrImg);
}
else
{
   echo "no matches";
}

See demo

Regular expressions can be a challenging subject even for experienced developers. So, it's important to understand what makes a decent regular expression and what is unnecessary. Also, there is a concept of "greediness", i.e. sometimes you can get back more than what you ask for. In this particular case, all that the OP wishes to attain are the file names of the two images.

So, the regular expression is much easier than what one might have imagined. The winning pattern searches for one or more word characters represented by "\w", followed by the escape sequence for a digit, namely "\d", and then followed by a period. The period must be escaped or otherwise it gets interpreted as any other character. Lastly, the file name extension consists of three alphabetical characters which allows for either a file extension of "jpg" or "png".

To extract that array with the file names of the images from the first element of the array $matches, the code uses array_pop().

Upvotes: 1

Related Questions