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 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.
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]);
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.