SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_match help please

    Hi Guys!

    I am using CURL to retrieve the results from a page and on that page is 2 hidden form fields. What I need to do is get the value of the hidden form fields. So basically what I need is some preg_match code to get the value from the following fields:

    PHP Code:
    <input type="hidden" name="cvref" value="3575469">
    <
    input type="hidden" name="filekey" value="jZDV9dr8DETkD1s4"
    So what I need to do is extract the values from the above fields. In short I need the following to be returned from the preg_match:

    PHP Code:
    3575469
    jZDV9dr8DETkD1s4 
    Thanks.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Is there a reason you're not using the DOM to read the elements required?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Is there a reason you're not using the DOM to read the elements required?
    I'm using CURL() like this and it's returning a string:

    PHP Code:
    $curl_handle curl_init();
                
    // Set the URL to be processed
                
    curl_setopt($curl_handleCURLOPT_URL"http://www.post-url.com/register.php");
                
    // Assign the data to be sent
                
    curl_setopt($curl_handleCURLOPT_POSTFIELDS$data);
                
    // Return details
                
    curl_setopt($curl_handleCURLOPT_RETURNTRANSFER1);
                
    // Set result var
                
    $page_1_result curl_exec($curl_handle); 

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Don't (or at least don't default to it) use RegExp to parse a structured document, there are tools out there better suited to it.

    See DOMDocument and DOMXPath .

    PHP Code:
    <?php
    $html 
    '
    <html>
      <head>
        <title>Demo</title>
      </head>
      <body>
        <form action="" method="post">
          <input type="hidden" name="foo" value="foo-value" />
          <input type="hidden" name="bar" value="bar-value" />
        </form>
      </body>
    </html>
    '
    ;

    $doc = new DOMDocument;
    $doc->loadHTML($html);

    $xpath = new DOMXPath($doc);

    $foo $xpath->query("//input[@type='hidden' and @name='foo']/@value")->item(0)->nodeValue#foo-value
    $bar $xpath->query("//input[@type='hidden' and @name='bar']/@value")->item(0)->nodeValue#bar-value

    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    PHP Code:
    <?php
    $html 
    '
    <html>
      <head>
        <title>Demo</title>
      </head>
      <body>
        <form action="" method="post">
          <input type="hidden" name="foo" value="foo-value" />
          <input type="hidden" name="bar" value="bar-value" />
        </form>
      </body>
    </html>
    '
    ;

    $doc = new DOMDocument;
    $doc->loadHTML($html);

    $xpath = new DOMXPath($doc);

    $foo $xpath->query("//input[@type='hidden' and @name='foo']/@value")->item(0)->nodeValue#foo-value
    $bar $xpath->query("//input[@type='hidden' and @name='bar']/@value")->item(0)->nodeValue#bar-value

    ?>
    I am using CURL() because im posting data to another website on a different server. So I guess, I can't use your method?

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Why would you think that? The fact you're using curl is irrelevant, you're performing operations on the 'string' returned. Namely $page_1_result .

    Read the documentation, it should clear things up for you.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Why would you think that? The fact you're using curl is irrelevant, you're performing operations on the 'string' returned. Namely $page_1_result .

    Read the documentation, it should clear things up for you.
    Okay I see. I tried your suggestion, but im getting the following error when trying to load the values of $page_result_1:

    PHP Code:
    WarningDOMDocument::loadHTML() [function.DOMDocument-loadHTML]: htmlParseEntityRefno name in Entityline522 in /home/myuser/public_html/mydomain.com/public/users/register-cv-library.php on line 104 

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    As it is a warning, you maybe able to safely ignore it. Does the rest of the code function as normal...?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    As it is a warning, you maybe able to safely ignore it. Does the rest of the code function as normal...?
    No, code is not working...there are lots of these errors (probably about 20 or more on the page).

  10. #10
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of-course DOM is the best way to achieve what you are trying to do.
    Ignore all those warning or put @ in front of loadHTML() to suppress all warnings.

    echo the two variables $foo and $bar in the end.

    If you dont get any values try without xpath, check this. HTML Parsing in PHP.

  11. #11
    SitePoint Member
    Join Date
    Aug 2010
    Location
    United States
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are much better ways to accomplish the task like the others have said. But here's the regular expression to use: (tell me if it doesn't work, I might have messed up on the quotes)

    Code:
    // Assuming $html_code is the HTML of the entire page
    
    $cvref = preg_replace('/^.*name\="cvref"\s+value\="(.+)"\>.*$/', '$1', $html_code);
    
    $filekey =  preg_replace('/^.*name\="filekey"\s+value\="(.+)"\>.*$/', '$1', $html_code);

  12. #12
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    I'm going to play the little devil (or evil leprechaun) on your opposite shoulder to Anthony.

    Quote Originally Posted by AnthonySterling View Post
    Don't (or at least don't default to it) use RegExp to parse a structured document, there are tools out there better suited to it.
    Sound advice, but I might go so far as to say that Zaggs is not trying to parse a structured document; in the sense that one might need a tool to provide a programmatic interface to a whole document. Consider the following, almost identical but different in a slight yet meaningful way, snippet based on the Zaggs' HTML snippet.

    Code:
    [input type="hidden" name="cvref" value="3575469"]
    [input type="hidden" name="filekey" value="jZDV9dr8DETkD1s4"]
    See what I did?

    Off Topic:

    Normally, I would too be offering a DOMó(or other more suitable tool)óbased answer but the bandwagon is getting tired and old. Those bright young whipper-snappers who, frequenting the places where such topics are raised, so confidently bemoan the practicality of regexes within 50 paces of the acronym HTML are readily becoming quite wearisome.


    P.S. If Zaggs likes the look of using the DOM extension to get at his required values, I'll be happy to sidle up to Anthony to help arrive at a nice solution.
    Salathe
    Software Developer and PHP Manual Author.


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
  •