[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

PHP Parsing


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?

  • 3
  • 2
1 Solution
$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
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
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.
$regex = '/value="(.*?)"/';
$regex = '/<input type=hidden name=ALTNUMBER value="(.*?)"/';
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

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

Muhammad Wasif

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now