Page 1 of 1

Understanding $pattern syntax

Posted: Fri Jan 25, 2008 4:28 am
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...

Re: Understanding $pattern syntax

Posted: Fri Jan 25, 2008 4:38 am
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.