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
LVL 1
niftyhawkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BogoJokerCommented:
<?php
$str = '<input type=hidden name=ALTNUMBER value="P80266">';
$regex = '/value="(.*?)"/';
preg_match($regex, $str, $matches);
$value = $matches[1];
print $value . ' is what you want!';
?>

Enjoy =)
Joe P
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BogoJokerCommented:
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
0
Muhammad WasifCommented:
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="(.*?)"/';
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

niftyhawkAuthor Commented:
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.. :)
0
BogoJokerCommented:
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
0
Muhammad WasifCommented:
Hi Joe,

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

Muhammad Wasif
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.