Page 1 of 1
var1: val1 var2: val2
Posted: Sun Mar 04, 2007 11:22 am
by nwp
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 ??
Posted: Sun Mar 04, 2007 11:24 am
by feyd
preg_match_all().
edit: also be aware of the characters \w matches. It's probably more than you think.
Posted: Sun Mar 04, 2007 11:57 am
by nwp
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
Posted: Sun Mar 04, 2007 6:03 pm
by feyd
Your pattern requires adjustment.
Posted: Sun Mar 04, 2007 9:44 pm
by nwp
feyd wrote:Your pattern requires adjustment.
sorry i didn't understand. please explain . and tell me a solution.
Posted: Sun Mar 04, 2007 9:50 pm
by feyd
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.
Posted: Sun Mar 04, 2007 9:53 pm
by nwp
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 ??
Posted: Sun Mar 04, 2007 10:03 pm
by feyd
Try stuff. Do you understand what each of the metacharacters do in the pattern?
Posted: Sun Mar 04, 2007 10:10 pm
by nwp
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
Posted: Sun Mar 04, 2007 10:12 pm
by wildwobby
im a n00b, but I think the ^ indicates it is at the start of a string, therfore only the first one will match
Posted: Mon Mar 05, 2007 12:36 am
by Chris Corbyn
It appears you're trying to parse JSON. If you are, there are libraries available for this.
Posted: Mon Mar 05, 2007 5:31 am
by nwp
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 ??
Posted: Mon Mar 05, 2007 6:42 am
by Kieran Huggins
check the bottom of
http://json.org/ for an exhaustive list
also, as of php 5.2:
http://php.net/json