Link to home
Start Free TrialLog in
Avatar of niftyhawk
niftyhawk

asked on

PHP Parsing

Hi,

I have an HTML string consisting of a lot of HTML code with a bunch of input tags and select tags. In between the HTML, I have a line saying..

<input type=hidden name=ALTNUMBER value="P80266">

The value tag keeps changing on every page load, so I cannot stick to one number.

I need to write a php code to parse the above tag and strip the value tag into a variable.

How do I do that?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of BogoJoker
BogoJoker

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BogoJoker
BogoJoker

Here is an explaination of the code.
1) the string $str, that is what you gave me to work with, I need to parse that for the value, which is P80266.
2) the regular expression I use to grab that value.  The regex is: /value="(.*?)"/
What that means is find the exactly this --> value="
Followed by any characters until you reach --> "
So it will match ANYTHING in between those double quotes: value="blahblahblae"
3) I put the match, which in this case is actually --> value="P80266"
into an array called $matches.  Exactly that will be $matches[0].
4)A nifty trick I learned from Roonaan, an expert here is that by putting those parenthesis in the regex, (.*?) now whatever that part of the regex was will be in $matches[1] becaue that is the first "atom" of the regex.  So it was a nice little shortcut with the preg_match function. $matches[1] will be P80266

Given an example: <input type=hidden name=ALTNUMBER value="a_b_c_1231&^">
Then $matches[0] is: value="a_b_c_1231&^"
And $matches[1] is: a_b_c_1231&^

Hope that helps you understand the code, if I was unclear about anything or if this confused you at all just ask =)
Joe P
A little modification to Joe's code

If you supply the following string, Joe's code will return 123456 which is not required
$str = '<input type="text" value="123456" name="test"><input type=hidden name=ALTNUMBER value="P80266">';

to get the value for ALTNUMBER you should make a little change i.e.
Change
$regex = '/value="(.*?)"/';
to
$regex = '/<input type=hidden name=ALTNUMBER value="(.*?)"/';
Avatar of niftyhawk

ASKER

Thanks BogoJoker and Wasifg for your answers.. BogoJoker..I guess you expained the stuff very well. I finally understand regular expression usage :) You guys are great.. :)
Sure thing.
I agree with you wasifg that if the parameter on which my regex were to operate were different, it would not work.  I expected that the input would continually be the same but I was very narrow minded when I thought about that.

I should always be thinking general and abstract =)
Viewing your account, you registered on my birthday in 2002 wasifg!
Joe P
Hi Joe,

Its mean, my birthday at EE is yours too but in world;)

Muhammad Wasif