Php 如何获取字符串中的最后一个整数?

Php 如何获取字符串中的最后一个整数?,php,Php,我有这样的变量: $path1 = "<span class='span1' data-id=2>lorem ipsum</span>"; $path2 = "<span class='span2' data-id=14>lorem ipsum</span>"; <span class='span1' data-id='4'>Happy 25th birthday!</span> 有什么帮助吗?根据这些变量的设置方式,一

我有这样的变量:

$path1 = "<span class='span1' data-id=2>lorem ipsum</span>";
$path2 = "<span class='span2' data-id=14>lorem ipsum</span>";
<span class='span1' data-id='4'>Happy 25th birthday!</span>

有什么帮助吗?

根据这些变量的设置方式,一种解决方案可能是先获取整数,然后将其注入路径:

$dataId = 2;
$path1 = "<span class='span1' data-id='$dataId'>lorem ipsum</span>";
$dataId=2;
$path1=“lorem ipsum”;
(请注意,我在数据id值周围添加了引号,否则您的HTML无效。)

如果您不是自己构建字符串,而是需要解析它们,那么我不会简单地获取最后一个整数。原因是您可能会得到如下标记:

$path1 = "<span class='span1' data-id=2>lorem ipsum</span>";
$path2 = "<span class='span2' data-id=14>lorem ipsum</span>";
<span class='span1' data-id='4'>Happy 25th birthday!</span>
25岁生日快乐!
在这种情况下,字符串中的最后一个整数是25,这不是您想要的。相反,我将使用正则表达式来捕获数据id属性的值:

$path1 = "<span class='span1' data-id='2'>lorem ipsum</span>";
preg_match('/data-id=\'(\d+)\'/', $path1, $matches);
$dataId = $matches[1];
echo($dataId); // => 2
$path1=“lorem ipsum”;
预匹配('/data id=\'(\d+)\'/',$path1,$matches);
$dataId=$matches[1];
echo($dataId);//=>2.

您可以使用一个简单的正则表达式:

/data-id=[\"\']([1-9]+)[\"\']/g
然后您可以构建此函数:

function lastinteger($item) {
    preg_match_all('/data-id=[\"\']([1-9]+)[\"\']/',$item,$array);

    $out = end($array);

    return $out[0];
}
工作

完整代码:

function lastinteger($item) {
    preg_match_all('/data-id=[\"\']([1-9]+)[\"\']/',$item,$array);

    $out = end($array);

    return $out[0];
}

$path1 = "<span class='span1' data-id=2>lorem ipsum</span>";
$path2 = "<span class='span2' data-id=14>lorem ipsum</span>";

$a = lastinteger($path1); //2
$b = lastinteger($path2); //14
函数lastinteger($item){
preg\u match\u all('/data id=[\“\']([1-9]+)[\“\']/',$item,$array);
$out=结束($array);
返回$out[0];
}
$path1=“lorem ipsum”;
$path2=“lorem ipsum”;
$a=最后一个整数($path1)//2.
$b=最后一个整数($path2)//14
参考资料:

正则表达式教程:


创建正则表达式的好工具:

我认为您应该使用正则表达式,例如:

<?php
$paths = [];
$paths[] = '<span class=\'span1\' data-id=2>lorem ipsum dolor</span>';
$paths[] = '<span class=\'span2\' data-id=14>lorem ipsum</span>';
foreach($paths as $path){
    preg_match('/data-id=([0-9]+)/', $path, $data_id);
    echo 'data-id for '.$path.' is '.$data_id[1].'<br />';
}
函数查找($path){
$countPath=strlen($path);
对于($i=0;$i<$countPath;$i++){
if(substr($path,$i,3)=“-id”){
echo substr($path,$i+4,1);
}
}}查找($path1);

为此编写了一个适当的函数

function LastNumber($text){
    $strlength = strlen($text);   // get length of the string
    $lastNumber = '';             // this variable will accumulate digits
    $numberFound = 0;             

    for($i = $strlength - 1; $i > 0; $i--){   // for cicle reads your string from end to start
        if(ctype_digit($text[$i])){
            $lastNumber = $lastNumber . $text[$i];  // if digit is found, it is added to last number string;
            $numberFound = 1;  // and numberFound variable is set to 1
        }else if($numberFound){  // if atleast one digit was found (numberFound == 1) and we find non-digit, we know that last number of the string is over so we break from the cicle.
            break;
        }
    }
    return intval(strrev($lastNumber), 10); // strrev reverses last number string, because we read original string from end to start. Finally, intval function converts string to integer with base 10
}

我知道这已经被回答了,但是如果你真的想在没有特别提到属性或标签名的情况下得到最后一个int,考虑使用这个。

$str = "
asfdl;kjas;lkjfasl;kfjas;lf  999 asdflkasjfdl;askjf
<span class='span1' data-id=2>lorem ipsum</span>
<span class='span2' data-id=14>lorem ipsum</span>


Look at me I end with a number 1234
";


$matches = NULL;
$num = preg_match_all('/(.*)(?<!\d)(\d+)[^0-9]*$/m', $str, $matches);

if (!$num) {
  echo "no matches found\n";
} else {
    var_dump($matches[2]);
}

如果不想使用正则表达式,可以使用DOM API:

$dom = DOMDocument::loadHTML($path2);
echo $dom->getElementsByTagName('span')[0]->getAttribute('data-id');
$dom = DOMDocument::loadHTML($path2);
echo $dom->getElementsByTagName('span')[0]->getAttribute('data-id');