PHP Parsing

Posted on 2006-04-24
Last Modified: 2008-03-06

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?

Question by:niftyhawk
    LVL 17

    Accepted 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
    LVL 17

    Expert Comment

    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
    LVL 20

    Expert Comment

    by:Muhammad Wasif
    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="(.*?)"/';
    LVL 1

    Author Comment

    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.. :)
    LVL 17

    Expert Comment

    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
    LVL 20

    Expert Comment

    by:Muhammad Wasif
    Hi Joe,

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

    Muhammad Wasif

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now