SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem in Extracting values

    i want to extract value in following html code using php

    <input type="hidden" value="21" name="gameNo">

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RobinKang View Post
    i want to extract value in following html code using php

    <input type="hidden" value="21" name="gameNo">
    Assuming that the input tag is part of a form whose method attribute is 'post' and that the form has a submit button:
    PHP Code:
    $myExtractedValue $_POST['gameNo']; 
    Denny Schlesinger
    web services

  3. #3
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i want to extract it using preg match

  4. #4
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RobinKang View Post
    i want to extract it using preg match
    I hope someone else can help you because I absolutely don't dig regex.
    Denny Schlesinger
    web services

  5. #5
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,387
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    PHP Code:
    $html '<input type="hidden" value="21" name="gameNo">';
    $regex "/value=\"(\d+)\"/";
    preg_match($regex$html$matches); 
    $matches[1] will now contain the number 21. Note that this will only work with numbers, if the value of 'value' will be more than just numbers then you'd have to change the regex accordingly.

  6. #6
    Non-Member
    Join Date
    Mar 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hiiii,

    I strongly advise you to use a real HTML or XML parser for this instead. You cannot reliably parse HTML or XML with regular expressions
    Since your HTML code isn't XML-valid, consider the HTML Agility Pack, which I've heard is very good.
    Use this regress to extract value,
    "<span[^>]*>(.*?)</span>"
    I hope this will help you.

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    How exactly is that in any way shape or form going to extract a value from the Value attribute of an Input, Harshada?

    The DOM parser would be able to find this value for you, which is part of PHP. It would be better if the HTML had an ID tag on the element, but i'm guessing you're not in control of that.

    Standard Disclaimer: Make sure you have permission to scrape whatever data it is you're trying to.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •