Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.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 将字符串中最后3个数字与正则表达式匹配_Php_Regex - Fatal编程技术网

Php 将字符串中最后3个数字与正则表达式匹配

Php 将字符串中最后3个数字与正则表达式匹配,php,regex,Php,Regex,问题: <?php show_source('regex.php'); $string = " 780155OVERF I000000 TRANFER DOMESTIC 000114 STHLM SE AB "; ?> <!DOCTYPE html> <html> <head> <title>Regex to match l

问题:

<?php
    show_source('regex.php');

    $string = "
        780155OVERF I000000
        TRANFER DOMESTIC
        000114
        STHLM SE AB
    ";
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Regex to match last 3 numbers</title>
        <meta charset="utf-8">
    </head>
    <body>
        <?php
            echo nl2br(str_replace('/\d{3}(?=[^\d]+$)/g', '<span style="background-color:red;">$1</span>', $string));
        ?>
    </body>
</html>
尝试使用正则表达式突出显示字符串中的最后3个数字

代码:

<?php
    show_source('regex.php');

    $string = "
        780155OVERF I000000
        TRANFER DOMESTIC
        000114
        STHLM SE AB
    ";
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Regex to match last 3 numbers</title>
        <meta charset="utf-8">
    </head>
    <body>
        <?php
            echo nl2br(str_replace('/\d{3}(?=[^\d]+$)/g', '<span style="background-color:red;">$1</span>', $string));
        ?>
    </body>
</html>

正则表达式匹配最后3个数字

主要错误:
str\u replace
与正则表达式不兼容。使用
preg\u replace

$string = "
    780155OVERF I000000
    TRANFER DOMESTIC
    000114
    STHLM SE AB
";

// use `m` modifier as you have multiline string
// `g` modifier is not supported by preg_replace
echo preg_replace("/\d{3}(?=[^\d]+)$/m", '<span>$0</span>', $string);
$string=”
780155F I000000
国内运输
000114
STHLM SE AB
";
//使用'm'修饰符,因为您有多行字符串
//preg_replace不支持'g'修饰符
echo preg_replace(“/\d{3}(?=[^\d]+)$/m”,“$0”,“$string”);

主要错误:
str\u replace
与正则表达式不兼容。使用
preg\u replace

$string = "
    780155OVERF I000000
    TRANFER DOMESTIC
    000114
    STHLM SE AB
";

// use `m` modifier as you have multiline string
// `g` modifier is not supported by preg_replace
echo preg_replace("/\d{3}(?=[^\d]+)$/m", '<span>$0</span>', $string);
$string=”
780155F I000000
国内运输
000114
STHLM SE AB
";
//使用'm'修饰符,因为您有多行字符串
//preg_replace不支持'g'修饰符
echo preg_replace(“/\d{3}(?=[^\d]+)$/m”,“$0”,“$string”);
使用:

打印nl2br(
preg_replace('/\d{3}(?=[^\d]+$)/s',
'$0', 
$string)
);
使用:

打印nl2br(
preg_replace('/\d{3}(?=[^\d]+$)/s',
'$0', 
$string)
);

现在显示什么?
str\u replace
不处理REGEX使用
preg\u replace
它工作正常,现在显示什么?
str\u replace
不处理REGEX使用
preg\u replace
它工作正常