在 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
我特别需要从 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