Understanding $pattern syntax

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
maqeulek
Forum Newbie
Posts: 2
Joined: Fri Jan 25, 2008 4:20 am

Understanding $pattern syntax

Post by maqeulek »

Hello everyone,

I'm a PHP newbie and I'm having trouble understanding the pattern syntax in preg_match_all. For example, something like this:
(this pattern looks for image paths on a given url)

preg_match_all(“/src=[\"']?([^\"']?.*(png|jpg|gif))[\”‘]?/i”, $page_data, $images);

can someone please break down what all those characters mean in the pattern?

1) Why is there a “/” forward slash before src
2) What does a “?” mean?
3) what does ^ mean?
4) why is there /i on the end?
5) what are the (, [, ., * and other special characters?

I went through http://de3.php.net/manual/de/reference. ... syntax.php
but its also hard for me to understand...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Understanding $pattern syntax

Post by Chris Corbyn »

It's called a regular expression (often shortened to Regex, or RegExp). Take a look at our "Regex" board here on the forum, then read the first sticky in there named CRASH COURSE Pt 1.
Post Reply