I'll-Be-Back
I'll-Be-Back

Reputation: 10828

preg_match is not matching

I am trying to get the ID from transaction(123456) in the HTML

It doesn't seem to return anything.

 $response = "</body> </html><script type='text/javascript'>transaction(123456);</script>";

 preg_match('/^transaction\((\d+)\)/', $response, $match);

 print_r($match);

if (is_numeric($match[1])) {
  echo "Your ID: " . $match[1];
}

Upvotes: 1

Views: 1748

Answers (3)

tuxtimo
tuxtimo

Reputation: 2790

$response = "</body> </html><script type='text/javascript'>transaction(123456);</script>";

preg_match('/transaction\((\d+)\)/', $response, $match);

print_r($match);

if (is_numeric($match[1])) {
  echo "Your ID: " . $match[1];
}

This is the code you want. The reason is, that your pattern started with "^" and this character means, that you expect taht "transaction" appears in the front of your string "$response", but there is "..."

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 60007

preg_match('/^transaction\((\d+)\)/', $response, $match);

should be

preg_match('/transaction\((\d+)\)/', $response, $match);

Upvotes: 1

nickb
nickb

Reputation: 59699

Because you have the start of string anchor ^, and transaction( isn't at the start of the string. Remove that (or add a non-greedy match) and it'll work (as shown by this demo):

preg_match('/transaction\((\d+)\)/', $response, $match);

Upvotes: 5

Related Questions