In PHP I have a string like this:
$string = "frame= 7840 fps= 53 q=0.0 size= 651693kB time=00:04:19.99 bitrate=20533.9kbits/s eta=00:00:22.63"
I need to extract the number 7840 into its own string. The problem is the number 7840 can be any number, like 1 or 2034321. The point is, whatever number is after frame= needs to be extracted to its own string. In PHP how would I accomplish this?
here is regex the right tool:
if (preg_match("#frame=\s*(\d+)#", $string, $match)) {
$number = $match[1];
}
This regular expression will do it:
if (preg_match('/frame=\s*(\d+)/', $string, $match)) {
$number = $match[1];
}
If it's really only that number you're after and the string always starts with "frame="
you can simply clip the string from that point onwards by using an (int)
typecast like this:
echo (int)substr($string, 6); // 7840
If there's any kind of variation in the beginning of your string I would highly recommend using regular expressions instead, which is already provided in other answers.