php - How can I get a unknown number from a string within a certain part of the string? -
my string:
how rate ease , comfort required undertake session?@question_value_0
how able value above string? don't know value (apart integer):
(some question)@question_value_x
x integer, want x.
i looked regex, suck @ regular expressions, i'm @ loss, cheers guys!
about far got regex
/@question_value_[0-9]+/
but can't number out of string. how can grab number?
this should work you:
just put escape sequence
\d (which means 0-9) quantifier
+ (which means 1 or more times) group (()) capture the number can access in array $m
.
<?php $str = "how rate ease , comfort required undertake session?@question_value_0"; preg_match("/@question_value_(\d+)/", $str, $m); echo $m[1]; ?>
output:
0
if print_r($m);
see structure of array:
array ( [0] => @question_value_0 [1] => 0 )
and see ^ have full match in first element , first group ((\d+)) in second element.
Comments
Post a Comment