I know regex basics well. But I couldn't figure out following problem while I doing my mini project. Let me explain you a bit:
The following is given in normal html file:
{
"Subject": "English 101",
"name": "Dave More",
"Grade": "A-",
}
What I was asked to do is that to grab name info and to echo it by php.
what I tried is that
preg_match_all('/name\W\W\s\W([^"]+)\W/', $school, $name); // $school is where the html is stored
echo $name;
I just wanted the output to be:
Dave More
but it give me out Array.
Would you mind to co-operate? plz
thanks
I need simple help on Regex
Moderator: General Moderators
Re: I need simple help on Regex
poking around for 2/3 days for an answer. Anyone?
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: I need simple help on Regex
Look at the manual for preg_match_all(). What is populated in the $school variable? Also, you probably only need preg_match() unless you have multiple names.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: I need simple help on Regex
$School is populated with following:
{
"Subject": "English 101",
"name": "Dave More",
"Grade": "A-",
}
Please be specific in answer. I just told what I tried and actually tried bunch of preg_match combination. No luck yet. I know i'm lacking somewhere.
{
"Subject": "English 101",
"name": "Dave More",
"Grade": "A-",
}
Please be specific in answer. I just told what I tried and actually tried bunch of preg_match combination. No luck yet. I know i'm lacking somewhere.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: I need simple help on Regex
My bad. What I meant is what is stored in $name. The manual tells you and you can use print_r() or var_dump() to find out as well.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: I need simple help on Regex
If I do print_r($name) it gives output like:
Array ( [0] => Array ( ) [1] => Array ( ) )
And if I do var_dump($name) it gives output like:
array(2) { [0]=> array(0) { } [1]=> array(0) { } }
Btb, when I do the echo $school; it shows as follows: (for your reference)
{"Subject":"English 101","name":"Dave More","Grade":"A"}
May be I'm doing something wrong with regex. You you plz look at it?
Array ( [0] => Array ( ) [1] => Array ( ) )
And if I do var_dump($name) it gives output like:
array(2) { [0]=> array(0) { } [1]=> array(0) { } }
Btb, when I do the echo $school; it shows as follows: (for your reference)
{"Subject":"English 101","name":"Dave More","Grade":"A"}
May be I'm doing something wrong with regex. You you plz look at it?
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: I need simple help on Regex
Code: Select all
preg_match('/"name": "([^"]+)"/', $school, $name);
print_r($name);Code: Select all
$object = json_decode($school);
echo $object->name;mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: I need simple help on Regex
Thanks for your support. I guess I solved the problem.