I have a string that has a number inside exactly one pair of quotes. How can I get this number using php?
I have tried iterating over the string and or using str_split but I'm not sure how to limit it to what is inside the quotation marks. The fact that the number can be any length makes it even harder.
EDIT: Here is an example
Hello this is my "100"th string!,
I would need to return 100
;
preg_match()
is a way to get the number from string. Example:
$str = 'Hello this is my "100"th string!,';
preg_match('/\d+/', $str, $match);
echo $match[0];
echo intval('123string');
results in 123
trim the quotes from around the string, and php will immediately see the number inside.
$str = "'1234'";
$num = trim($str, "'\"");
echo $num + 1;
// => 1235
or if you have text as well a string, replace all non-digits with spaces, then have php automatically parse the string when it is used in an arithmetic expression.
$num = preg_replace('/\D/', ' ', $string) + 0;
use preg_replace:
echo preg_replace("~[^\d+]~", '', 'Hello this is my "100"th string!,');