在php中提取字符串的一部分


Extracting a part of a string in php

我特别需要从PHP输入中去除字符。

例如,我们有一个版本号,我只需要它的最后一部分

给定14.1.2.123,我只需要123
给定14.3.21,我只需要21

有没有一种方法可以让我在PHP中只得到这些数字?

你可以试试这个-

$temp = explode('.', $version); // explode by (.)
echo $temp[count($temp) - 1]; // get the last element
echo end($temp);

$pos = strrpos($version, '.'); // Get the last (.)'s position
echo substr(
     $version, 
     ($pos + 1), // +1 to get the next position
     (strlen($version) - $pos) // the length to be extracted
); // Extract the part

strrpos()、substr()、strlen()、explode()