Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中提取字符串末尾的值_Php - Fatal编程技术网

如何在php中提取字符串末尾的值

如何在php中提取字符串末尾的值,php,Php,在我的php代码中,我有一个变量数组,以一个单词开头,后跟一个(随机)数: 我知道我必须使用foreach循环,但是如何提取每个单词末尾的数字呢?(我假设我可以使用preg_match(),但不知道如何精确指定该函数?由于数字的长度不同,介于一位或两位数字之间,您可以这样使用: foreach( $array as $x) { preg_match( '/(\d{1,2})$/', $x, $match); echo "The number is: " . $match[1];

在我的php代码中,我有一个变量数组,以一个单词开头,后跟一个(随机)数:


我知道我必须使用foreach循环,但是如何提取每个单词末尾的数字呢?(我假设我可以使用preg_match(),但不知道如何精确指定该函数?

由于数字的长度不同,介于一位或两位数字之间,您可以这样使用:

foreach( $array as $x) {
    preg_match( '/(\d{1,2})$/', $x, $match);
    echo "The number is: " . $match[1];
}
但是,由于前缀是提前知道的,只需直接删除它(根据Marc B的评论,并举例说明用法):


由于数字的长度在一位或两位之间变化,您可以这样使用:

foreach( $array as $x) {
    preg_match( '/(\d{1,2})$/', $x, $match);
    echo "The number is: " . $match[1];
}
但是,由于前缀是提前知道的,只需直接删除它(根据Marc B的评论,并举例说明用法):

尝试使用此选项:(此选项适用于一位数字)

我已经更新了两位数的情况,没有regex看起来有点粗糙,但是如果出于某种原因您不想使用regex:(


如果“justaword”是常量,您可以使用
str_replace('justaword','',$x[0])以删除它。

尝试使用以下命令:(这适用于一位数字)

我已经更新了两位数的情况,没有regex看起来有点粗糙,但是如果出于某种原因您不想使用regex:(



如果“justaword”是常量,您可以使用
str_replace('justaword','',$x[0])
将其删除。

您可以尝试此操作。它仅适用于一个数字

foreach($x as $key => $value)
    echo substr($value,-1);
$str="justaword5";
echo $last_dig=substr($str,strlen($str)-1,strlen($str));

你可以试试这个。它只有一位数

foreach($x as $key => $value)
    echo substr($value,-1);
$str="justaword5";
echo $last_dig=substr($str,strlen($str)-1,strlen($str));

使用
str\u replace
删除前缀

$prefix = "justaword";
$words = array("justaword8", "justaword4", "justaword500");
$numbers = array();
foreach ($words as $word) {
    $numbers[] = str_replace($prefix, "", $word);
}
var_dump($numbers); // gives 8, 4, 500

使用
str\u replace
删除前缀

$prefix = "justaword";
$words = array("justaword8", "justaword4", "justaword500");
$numbers = array();
foreach ($words as $word) {
    $numbers[] = str_replace($prefix, "", $word);
}
var_dump($numbers); // gives 8, 4, 500
代码:

输出:

 `Hll Wrld f PHP`
相反,您应该做的是让数组成为所有26个字符的数组。 将所有字符替换为“”后,您可以直接将字符串转换为数字

代码:

输出:

 `Hll Wrld f PHP`
相反,您应该做的是让数组成为所有26个字符的数组。
将所有字符替换为“”后,您可以直接将字符串转换为数字

随机数总是一位数吗?或者可以是多个数字,例如12、122等。您知道前缀是什么单词吗?是否预先知道
justaword
并且是常量?然后只需使用一个子串操作,例如
substr('justaword8',strlen('justaword8'))
^或
str\u replace
@nickb:随机数可以是1或2位随机数始终是一位吗?或者可以是多个数字,例如12、122等。您知道前缀是什么单词吗?是否预先知道
justaword
并且是常量?然后只需使用一个子串操作,例如
substr('justaword8',strlen('justaword8'))
^或只是
str\u replace
@nickb:如果只有一个数字,随机数可以是1或2位。但OP说可能是两个。@JasonMcCreary为1或2个数字添加了解决方案如果只有一个数字就好了。但OP说可能是两个。@JasonMcCreary为1或2个数字添加了解决方案,我认为这是
stru替换($prefix,“,$word)。我认为它是str_replace($prefix,“,$word)。还不知道substr()。对于快速评论和建议(!)的Thnx(all)还不知道substr()。Thnx(全部)用于快速评论和建议(!)