3

I have this field Returned by curl_exec:

<input name="NUMBER_R" type="hidden" value="1500000">

150000 is a random number and may change the others are constant

i tried:

preg_match ('/<input name="NUMBER_R" type="hidden" value="([^"]*)" \/>/', $result, $number)

and also:

preg_match ('/<input name=\'NUMBER_R\' type=\'hidden\' value=\'(\\d+)\'>/ims', $result, $number)

but no luck...

Here is the full code:

$result=curl_exec($cid);
curl_close($cid);
$number = array();
if (preg_match ('REGEX', $result, $number))
  {
    echo $number[1];
  }

EDIT 1: Sorry i forgot [1] in echo $number[1]; Also 1500000 is a random number and may change

Vladimir
  • 1,602
  • 2
  • 18
  • 40
  • possible duplicate of [Parsing and processing HTML/XML?](http://stackoverflow.com/questions/3577641/parsing-and-processing-html-xml) – Quentin Jun 10 '13 at 11:56
  • 3
    In the first attempt, you're forcing a self-closing tag, whereas the returned value is actually not self-closing. Also, REGEX'ing elements is generally not a great move. Why not throw it into a DOMDocument and read the value out of that? – Mitya Jun 10 '13 at 11:57
  • `$number` is an array, you should use `print_r` instead of `echo`. – HamZa Jun 10 '13 at 11:57
  • @Utkanos Thank you that actually worked, unfortunately i can't use DOM for some reason, just another question, is the value=\(\\d+)\ ok if im always getting a number? what is better to use if its always a number? i wish you answer instead of comment so i can accept it – Vladimir Jun 10 '13 at 12:06
  • 1
    I didn't answer as I didn't imagine that the self-closing thing would be your ultimate eproblem. Yes, `\d` is perfect for matching numbers - that's what it's for. Only thing to keep in mind is your `+` modifier will require that there's at least one number. If there's a chance of there being no value at all, but you still want the pattern to match, use `*`, which enforces 0 or more. – Mitya Jun 10 '13 at 12:54

2 Answers2

4

Description

This regex will find the input tag which has the attributes name="number_r" and type="hidden" in any order. Then it'll pull out the attribute value with it's associated values. It does require the value text to be all digits

<input\b\s+(?=[^>]*name=(["'])number_r\1)(?=[^>]*type=(["'])hidden\2)[^>]*value=(["'])(\d+)\3[^>]*>

enter image description here

  • <input\b\s+ consume the open bracket and the tag name, ensure there is a word break and white space
  • (?=[^>]*name=(["'])number_r\1) look ahead to ensure this tag include the correct name attribute
  • (?=[^>]*type=(["'])hidden\2) look ahead to ensure this tag also includes the type attribute
  • [^>]* move the cursor forward until we find the
  • value= tag
  • (["']) capture the open qoute
  • (\d+) capture the substring and require it to be all digits
  • \3 match the correct close quote. This is can be omitted as you've already received the desired substring.
  • [^>]*> match the rest of the characters in the tag. This is can be omitted as you've already received the desired substring.

Groups

Group 0 gets the entire input tag

  1. the open quote for name which is back referenced to ensure the correct close quote is captured
  2. the open quote for type which is back referenced to ensure the correct close quote is captured
  3. the open quote for value which is back referenced to ensure the correct close quote is captured
  4. the value in the attribute named value

PHP Code Example:

<?php
$sourcestring="<input name="NUMBER_R" type="hidden" value="1500000">";
preg_match('/<input\b\s+(?=[^>]*name=(["\'])number_r\1)(?=[^>]*type=(["\'])hidden\2)[^>]*value=(["\'])(\d+)\3[^>]*>/im',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>

$matches Array:
(
    [0] => <input name="NUMBER_R" type="hidden" value="1500000">
    [1] => "
    [2] => "
    [3] => "
    [4] => 1500000
)
Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43
0

Try using DOM and Xpath for get that.

$xml = new DomDocument;
$xml->loadXml('<input name="NUMBER_R" type="hidden" value="1500000" />');
$xpath = new DomXpath($xml);
// traverse all results
foreach ($xpath->query('//input[@name="NUMBER_R"]') as $rowNode) {
   var_dump($rowNode->getAttribute('value'));
}

testet : http://codepad.viper-7.com/8dwu9f

  • 1
    thank you so much, but unfortunately i can't use DOM for that, i just need a pattern for a hidden field with the value of a random number – Vladimir Jun 10 '13 at 12:09