I'm trying to match the numbers inside ('')
$linkvar ="<a onclick="javascript:open('597967');" class="links">more</a>"
preg_match("^[0-9]$",$linkvar,$result);
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]);
Your regex only matches if the entire string is made up of one number because of the ^
and $
modifiers. Your current regex translates in human language to:
^
means "this is the start of the string"[0-9]
means "match a single numeric character"$
means "this is the end of the string"Change it to:
preg_match("[0-9]+",$linkvar,$result);
Or alternatively, the shorthand syntax for matching numbers:
preg_match("\d+",$linkvar,$result);
The +
modifier means that "one or more" numbers must be found in order for it to be a match.
Additionally, if you want to actually capture the numbers inside the string you'll need to add parentheses to inform preg_match
that you actually want to "save" the numbers.
The ^ and $ match the start and end of the string, which means you are currently searching for a string containing ONLY a single digit. Remove them and add a plus quantifier, leaving just "[0-9]+", and it will find the first group of digits in the string.
preg_match("[0-9]+",$linkvar,$result);