Search code examples
phpregexhtml-parsingpreg-matchtext-extraction

Get the number from inside of online javascript function call within an HTML element


I'm trying to match the whole number inside of open(''), but I am getting the error :

Warning: preg_match(): No ending delimiter '^' found

Here is my code:

$linkvar ="<a onclick=\"javascript:open('597967');\" class=\"links\">more</a>";

preg_match("^[0-9]$", $linkvar, $result); 

Solution

  • Your regex will only match if the string is exactly one digit. To match only the digits inside the quotes, use:

    preg_match("/'(\d+)'/", $linkvar, $result);
    var_dump($result[1]);