Find two different substr in PHP

From:

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

      

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

Something like that:

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

      

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

+3


source to share


2 answers


This fits your intended image files with less flexibility (improved precision) in the file suffix and more flexibility in the filename.

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

This would correspond to one or more letters / numbers / symbols underscore followed by point, then gif

, png

, jpg

or jpeg

(insensitive).

If you need more file suffixes, you can expand the expression with another alternative by adding a pipe ( |

) and then your suffix.

If you need to include more possible characters at the beginning of the file name, you can create a character class like this [\w@!]

, which will also allow @

and !

. Or you can change \w

to \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 find that by tightening the file suffix requirements, you can avoid unintentional matches that are not image files.

+1


source


Updated answer:

<?php

$str = "This is one string with this picture pic1.jpg and here there 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 tricky topic, even for experienced developers. So it's important to understand what a decent regex does and doesn't need to. In addition, there is the concept of "greed", that is, sometimes you can return more than what you ask for. In this particular case, all the OP wants to achieve is the filenames of the two images.

So, regex is much simpler than you might imagine. The winning pattern searches for one or more word characters denoted by "\ w" followed by an escape sequence for a digit, namely "\ d", followed by a period. The period must be escaped or otherwise interpreted like any other character. Finally, the file name extension is made up of three alphabetic characters, which allow either the "jpg" or "png" file extension.

The code uses array_pop () to extract this array of image filenames from the first element of the $ matches array.

+1


source







All Articles