I am new to php and was confused by the preg_match_all() function.
Could someone explain what each part of the function is doing in this example?
preg_match_all("/<item><title>([^<]*) - ([^<]*?)<\/title>/i", $buffer, $titlematches);
I am new to php and was confused by the preg_match_all() function.
Could someone explain what each part of the function is doing in this example?
preg_match_all("/<item><title>([^<]*) - ([^<]*?)<\/title>/i", $buffer, $titlematches);
 
    
     
    
        /([^<]) - ([^<]?)<\/title>/i
1st Capturing group ([^<])
[^<] match a single character not present in the list below
< a single character in the list < literally (case insensitive)
 -  matches the characters  -  literally
2nd Capturing group ([^<]?)
[^<]? match a single character not present in the list below
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
< a single character in the list < literally (case insensitive)
< matches the characters < literally
\/ matches the character / literally
title> matches the characters title> literally (case insensitive)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
 
    
    This is a regular expression, and they are tricky bastards until you get used to them. They are not just for PHP, every language has functionality to use them.
This is searching $buffer, looking for a <title> element inside an <item> element. It's looking inside the <title> element for two blocks of text separated by - (the second block is optional.) The found blocks of text are saved into $titlematches for use in the script.
As mentioned in the other answer, http://regex101.com/ is a good resource to check your syntax out, but maybe not for beginners though!
