Jump to content

Preg_match help


mifflin

Recommended Posts

can someone explain a few things for me?  and yes I googled it, but I just had a few questions. 

 

In preg_match, "(.+?)" could be basically anything, "/^" means no slash, but what does "\|\|\|/" mean? slash or slash or slash or forward slash?  I'm not getting what this does....  look.

 

[pre]

preg_match("/^(.+?)\|\|\|/", $obj->images, $matches);

if (!isset($matches[1])) {

preg_match("/^(.+?)\|(.+?)\|\|/", $obj->images, $matches);

$img = $matches[1];

$img_align = $matches[2];

[/pre]

 

 

and, I know this is a dumb question, but what does the " $obj->images, $matches);" do after the preg_match

 

 

Thanks in advance. 

Link to comment
Share on other sites

No, you misunderstand..

 

(.+?) - the dot is a wildcard that matches a single character (other than a newline  by default). the + is a one or more times quantifier, and the ? makes the + lazy (it will not be greedy) The parenthesis that encases this all is a capture, which means whatever it matches in .+? will be stored into the value $1.

 

/^ The / is actually the beginning delimiter (you'll notice that the pattern starts and ends with /.. so after the opening delimiter, the ^ means from the start of the string.

 

\|\|\| is a way of saying, match 3 consecutive pipes... which could be re-wirtten as \|{3}.. the backslash escapes the pipe character so that the pattern doesn't see it as OR, as the pipe is used for such purposes, as in (ab|bc) which would mean, capture the letters ab OR bc.

 

As far as $obj->images is concerned, this is an object containing a property called images (google OOP - Object Oriented Programming).

The $matches means what ever the pattern matches, it will be stored into the array called, well, $matches. The complete pattern would be stored as $matches[0]. So if there is parenthesis involved, the first set will be stored into $matches[1], if there is a second set of parenthesis, this will be stored into $matches[2], etc.. (which runs in parellel with $1, $2, etc.. (hope I'm clear in my explanations here).

 

You can have a look at these regex resources to better understand what's going on:

 

http://www.regular-expressions.info/

http://weblogtoolscollection.com/regex/regex.php

http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1239475391&sr=8-1

pcre

http://www.phpfreaks.com/forums/index.php/topic,127902.0.html

http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax

 

This will be more than enough to get you started.

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.