Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Sun Mar 04, 2007 11:22 am
I need a regex To make an array of vars and vals from "var1: val1 var2: val2 :........."
I was trying this
Code: Select all
<?php
$string = "var1: val1 var2: val2";
header("Content-Type: text/plain");
preg_match("/^(\w+):[\s]*(\w+)/", $string, $matches);
print_r($matches);
?>
Browser wrote: Array
(
[0] => var1: val1
[1] => var1
[2] => val1
)
But What about the var2 and val2 and others !
How can I do it ??
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Mar 04, 2007 11:24 am
preg_match_all() .
edit: also be aware of the characters \w matches. It's probably more than you think.
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Sun Mar 04, 2007 11:57 am
I tried preg_match_all() But it didn't worked also
Code: Select all
<?php
$string = "var1: val1 var2: val2";
header("Content-Type: text/plain");
preg_match_all("/^(\w+):[\s]*(\w+)/", $string, $matches, PREG_SET_ORDER);
print_r($matches);
?>Browser wrote: Code: Select all
Array
(
[0] => Array
(
[0] => var1: val1
[1] => var1
[2] => val1
)
)
Its Not Matching Var2 or var3
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Mar 04, 2007 6:03 pm
Your pattern requires adjustment.
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Sun Mar 04, 2007 9:44 pm
feyd wrote: Your pattern requires adjustment.
sorry i didn't understand. please explain . and tell me a solution.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Mar 04, 2007 9:50 pm
You must adjust the pattern so it is able to find more than one solution. Currently, your pattern will only allow a single solution given the test string.
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Sun Mar 04, 2007 9:53 pm
feyd wrote: You must adjust the pattern so it is able to find more than one solution. Currently, your pattern will only allow a single solution given the test string.
But How ??
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Mar 04, 2007 10:03 pm
Try stuff. Do you understand what each of the metacharacters do in the pattern?
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Sun Mar 04, 2007 10:10 pm
feyd wrote: Try stuff. Do you understand what each of the metacharacters do in the pattern?
No Please show me an example how i can solve my problem
wildwobby
Forum Commoner
Posts: 66 Joined: Sat Jul 01, 2006 8:35 pm
Post
by wildwobby » Sun Mar 04, 2007 10:12 pm
im a n00b, but I think the ^ indicates it is at the start of a string, therfore only the first one will match
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Mon Mar 05, 2007 12:36 am
It appears you're trying to parse JSON. If you are, there are libraries available for this.
nwp
Forum Contributor
Posts: 105 Joined: Sun Feb 04, 2007 12:25 pm
Post
by nwp » Mon Mar 05, 2007 5:31 am
d11wtq wrote: It appears you're trying to parse JSON. If you are, there are libraries available for this.
Really ??
Actually I was going to build it by myself cause i was thinking that there are no librery for jason in php .
So Would You So an Example ??