Computer Science Canada Preg_Match_All ............ syntax error! |
Author: | samara [ Tue Jun 23, 2009 8:27 am ] | ||
Post subject: | Preg_Match_All ............ syntax error! | ||
Hai need help regarding this one: I think the problem is with the $pattern2, I am not knowing the format to write it!! Can anyone help me please This is the code, my task is to pull out the values of the report type stired in the file: They are stored like this: username ~false./true.report type1~false./true.report type2.......etc the report type will be added to a file with a false or true before it depending on user selection in previous step. Quote: $pattern2 = "/^([-\w]{".PERM_TYPE_SEPARATOR."})\s(false.|true.)\s(.*)$/";
if(preg_match_all($pattern2, $entry2, $match)) { echo "this one is called too"; for($i = 0; $i < count($match[1]); $i++) { echo "This one"; //if($match[1][$i] == "true.") echo $match[2][$i]; } //echo "123"; //$metadata[$matches[1][$i]] = $matches[2][$i]; } Value of $entry2 at the time of execution
This is the result I am getting Quote: $match =array
0 => array empty 1 => array empty 2 => array empty 3 => array empty This is the text file I am actually parsong: admin ~false.123~false.45678~false.67890~false.123456~false.Hai testing~false.I am good~false.Now this should work~false.Second time~false.Test~false.Testing Parsed list~false.Testing reports~false.Testing settings~false.abcd~false.abcdef~false.hai how are you~true.sawraasdagweagsd~true.tested~true.tested2~true.testing~true.testing code again~true.testing code again 2~true.testing code now~true.testing it again~true.testing new report~true.third one~false.ujruesszerhqwrqhwer |
Author: | DtY [ Tue Jun 23, 2009 10:29 am ] |
Post subject: | RE:Preg_Match_All ............ syntax error! |
Few things, 1) '.' is a regular expression meaning any printable character, if you want to look for a period, you need to use '\.' 2) What is the point of the {}? You're in a double quoted string, so that will use PHP's interpolation, before the regular expression sees it. If that's what you're going for, you probably want a variable in there, not a string. 3) You're in a double quoted string, so you need to be careful about what you escape. I don't think anything you have there will be a problem for PHP, but it's always better to use signle quoted strings in regular expressions. 4) You will want to make a bunch of those expressions lazy, rather than greedy. Lazy expressions will stop searching as soon as it can, greedy will take as much as it can. Use ? to make a repeating expression lazy. For example, say you have the string: "abc.123.def", and you want to find everythign up to the period (Something like you're doing, I think?), if you use the expression: /(.+)\./ it will match "abc.123" (up to the last period), however: /(.+?)\./ will match only "abc" because it is lazy, and stops as soon as it can. |
Author: | samara [ Tue Jun 23, 2009 2:24 pm ] |
Post subject: | Re: Preg_Match_All ............ syntax error! |
i was not able to figure out from what you said, can you please correct the value of $pattern2 and post it here!! Thanks!! now i have this one: ~false.123~false.45678~false.67890~false.123456~false.Hai testing~false.I am good~false.Now this should work~false.Second time~false.Test~false.Testing Parsed list~false.Testing reports~false.Testing settings~false.abcd~false.abcdef~false.hai how are you~true.sawraasdagweagsd~true.tested~true.tested2~true.testing~true.testing code again~true.testing code again 2~true.testing code now~true.testing it again~true.testing new report~true.third one~false.ujruesszerhqwrqhwer i need a $pattern value, which takes out the ~ false and Report name!! Pls help me!! |
Author: | DtY [ Tue Jun 23, 2009 3:34 pm ] | ||||
Post subject: | RE:Preg_Match_All ............ syntax error! | ||||
So what you're trying to do is separate is into an array like: [[false. 123], [false, 45678]...]? That can be achieved without regexp:
On your first string, that will give you:
|