Pergunta

I am trying to match an "ID" in a string, composed by some numbers (ranging from 1 digit to ~9 (the point is that it has got a variable length). The following code won't backreference my ID, I'm very new to PHP and I've tried google with no good answer.

<?php
$to = 'data-hovercard="/ajax/hovercard/user.php?id=100002781344760">Ae fj';

preg_match('/user.php?id=[\d]+\\"/', $to, $matches);

echo $matches[0];
?>

The whole point is to get the "10000278134476" in "/ajax/hovercard/user.php?id=100002781344760">". Any useful regexes?

Foi útil?

Solução

Adding ()s to mark groups should work, or if you want to make it more meaningful you can try named groups with (?<name>):

preg_match('/user.php?id=([\d]+)\\"/', $to, $matches);
//                       ^     ^
echo $matches[1]; // capture groups indexed from 1, 0 index is for the whole matched string

See the docs for more on the pattern sytnax.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top