开发者

Confusion with preg_match

this is probably very easy question...

I don't really know how to use pattern search (preg_match) and I am faced with code which includes it...

if (preg_match('!http://.*?/photos/.*?/(\d+)!i', 'http:/example.com/sites/default/files/1301584398_file.jpg', $match)) {
    $id = $match[1];
    $info = $f->photos_getInfo($id);
    $sizes = $f->photos_getSizes($id);
    foreach($sizes as $size => $sizedata) {
        if ($size == "Large" || $size == "Original") {
            $source = $sizedata['source'];
            if (preg_match('/\.(jpg|jpeg)$/i', $source)) {
                $jpg = imagecreatefromjpeg($source);
            }
            if (preg_match('/\.png$/i', $source)) {
                $jpg = imagecreatefrompng($source);
            }
            if (preg_match('/\.gif$/i', $source)) {
              开发者_运维知识库  $jpg = imagecreatefromgif($source);
            }
            break;
        }
    }

I am most confused by

preg_match('!http://.*?/photos/.*?/(\d+)!i', $url, $match)

What is this for? Since the input pattern have changed and I have no idea how it looked before it is a bit confusing...

The other function are pretty clear. They are meant to get the extensions of files.


!http://.*?/photos/.*?/(\d+)!i means a case insensitive match of http://.*?/photos/.*?/(\d+), where the pattern delimiter used was !.

http://.*?/photos/.*?/(\d+) matches any url having /photos/ in it, then something more, ending in / and some numbers.

.*? tries to match as little characters as possible, possible none at all. \d+ tries to match as much numbers as possible, with a minimum of 1.


preg_match() is a function used to do a regular expression match on a string, and store matches to an array.

The first parameter is a regular expression. It's a special syntax used to define patterns for matching text. I highly recommend the following link for learning regex syntax, as it's highly useful, although a bit cryptic at first and tricky to decipher.

Regular-Expressions.info

In your pattern:

  • !               - the regex delimiter
                       (usually a regex uses /, but we need to use slashes literally in our pattern).
  • http://   - a literal match of the string 'http://'
  • .*?           - match any character (.), zero or more times (*), and be 'lazy' (?).
                       (Lazy means: match the least possible num of chars while still satisfying
                       the whole pattern)
  • /photos/ - another literal string match
  • .*?           - same pattern as explained above. (0 or more of any char, tell regex engine to be lazy)
  • /               - A literal string match for forward slash '/'
  • ( )           - In regex, parentheses are a special character.
                       Used to Create a group to reference later as $match[1].
  • (\d+)       - Match one or more digits \d+, and create a group to contain the match.
  • i               - A flag used to tell the regex engine to ignore case.


This statement:

preg_match('!http://.*?/photos/.*?/(\d+)!i', $url, $match)

searches for the http:// string, followed by any character up to the first /, then photos/, then any other sequence of character up to another /, then a sequence of one or more digits which are recorded via backreference.

In other words, scan for http links whose second segment is photos and grab the numeric id at the end of the URL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜