Regexp does not take input value

Asked

Viewed 283 times

4

I need to extract the value of a tag html

<input value="2530317385" name="X-Tmx-session-id" id="X-Tmx-session-id" type="hidden">

I’m wearing a Expressão Regular to return the value of input

   $pattern = '~<input type=hidden id=X-Tmx-session-id name=X-Tmx-session-id value=(.*?) \/>~';
   preg_match($pattern, $get, $xArray);
   var_dump($xArray);

only that my expression returns only an empty array: array(0) { }

I just need to get the value of input

2530317385

  • You just return the value of that input? $_GET/$_POST doesn’t solve this?

  • The $get variable takes the html that comes from the Curl

  • 1

    @Lucascasterlamar your last Dit deformed the question. I restored the question. If you want you can delete the question or ask one of the moderators for help to delete it. If you have any questions, please comment here or at the goal (http://meta.pt.stackoverflow.com/) that we will assist you.

  • 1

    @Sergio I did not understand why he copied the answer code and put the question, I think he did not understand how Stackoverflow works. Rollback to Revision 3. Lucas is a community of questions and answers, if the answer code didn’t work you can comment by speaking the problem in the answer itself.

2 answers

3

Your Regex is different from your html-input, just compare:

 <input value="2530317385" name="X-Tmx-session-id" id="X-Tmx-session-id" type="hidden">
~<input type=hidden id=X-Tmx-session-id name=X-Tmx-session-id value=(.*?) \/>~

Missing quotes, position requested by Regex is different.

If you are getting an html by curl, why not use the DOM to pick up the elements? For example:

$get = '<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<input value="test1">
<input value="test2">
<input value="test3">
</body>
</html>';

$doc = new DOMDocument();
@$doc->loadHTML($get); //O @ previne mostrar erro de HTML

$allInputs = $doc->getElementsByTagName('input');
foreach ($allInputs as $input) {
    echo $input->getAttribute('value'), '<br>';
}

Or with Xpath (chance needs more advanced selectors):

<?php
$get = '<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <input value="2530317381" name="X-Tmx-session-id" id="X-Tmx-session-id" type="hidden">
    <input value="2530317382" name="X-Tmx-session-id" id="X-Tmx-session-id" type="hidden">
    <input value="2530317383" name="X-Tmx-session-id" id="X-Tmx-session-id" type="hidden">
    <input value="2530317384" name="X-Tmx-session-id" id="X-Tmx-session-id" type="hidden">
</body>
</html>';

$doc = new DOMDocument();
@$doc->loadHTML($get);

$xpath = new DOMXpath($doc);
$doc = NULL;

$allInputs = $xpath->query('*/input[@id=\'X-Tmx-session-id\']');

foreach ($allInputs as $input) {
    echo $input->getAttribute('value'), '<br>';
}

Online example: https://ideone.com/i9ZCsg

  • The two example me return error $allInputs = $dom->getelementsbytagname('input'); $allInputs = $xpath->query('*/input[@value]');

  • @Lucascasterlamar what a mistake?

  • With Xpath he returns nothing to me on the screen.

  • and in the first Fatal error: Call to a Member Function getelementsbytagname() on a non-object in / in that line $allInputs = $dom->getelementsbytagname('input');

  • @Lucascasterlamar fixed the two codes and added example with Xpath :)

  • I will edit my question with the code I used and following your example

  • @Lucascasterlamar my answer solved your problem? If yes mark the answer as correct, if not comment on what failed. If you don’t understand how Stackoverflow works, take a tour (you get a medal just for doing this rs): http://answall.com/tour

Show 2 more comments

0

I also recommend using the DOM library but prefer to use this.

Now in case you want to rat out the attempt to ER, try:

Example

$id = 'X-Tmx-session-id';

$pattern = "~(?=<input.*id=['\"]*{$id}['\"]*.*)<input.*value=['\"]*([^'\"]*)['\"]*.*~";

$get = '<input value="2530317385" name="X-Tmx-session-id" id="X-Tmx-session-id" type="hidden">';

preg_match($pattern, $get, $xArray);

echo "<pre/>";
var_dump($xArray[1]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.