Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.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 从字符串中查找最后5个字符并删除它们(如果它们包含&;符号)_Php_Regex_String - Fatal编程技术网

Php 从字符串中查找最后5个字符并删除它们(如果它们包含&;符号)

Php 从字符串中查找最后5个字符并删除它们(如果它们包含&;符号),php,regex,string,Php,Regex,String,我有一根绳子。使用PHP(以及最简单的解决方案,可能是preg_replace),我想: 从字符串中查找最后5个字符(不是单词) 如果最后5个字符中有一个包含“&”字符,我想删除此(&C)字符和后面的任何其他字符 例如,当字符串为: $string='Hello world this day and tomorrow'; 脚本应找到: “orrow” (由于“orrow”不包含“&”,所以不要执行任何操作) 但当: $string='Hello world this day and tomor

我有一根绳子。使用PHP(以及最简单的解决方案,可能是preg_replace),我想:

  • 从字符串中查找最后5个字符(不是单词)

  • 如果最后5个字符中有一个包含“&”字符,我想删除此(&C)字符和后面的任何其他字符

  • 例如,当字符串为:

    $string='Hello world this day and tomorrow';
    
    脚本应找到:

    orrow

    (由于“orrow”不包含“&”,所以不要执行任何操作)

    但当:

    $string='Hello world this day and tomor&row'

    $string='Hello world this day and tomo&rrow'

    $string='Hello world今明日&'

    $string='Hello world今明日&q'

    $string='Hello world今明日&co'

    等等。脚本应该删除&(包括&)之后的所有字符。

    这应该可以:

    for($i=max(0, strlen($string)-5);$i<strlen($string);$i++) {
        if($string[$i] == '&') {
            $string = substr($string,0,$i);
            break;
        }
    }
    
    for($i=max(0,strlen($string)-5);$i

    for($i=max(0, strlen($string)-5);$i<strlen($string);$i++) {
        if($string[$i] == '&') {
            $string = substr($string,0,$i);
            break;
        }
    }
    

    for($i=max(0,strlen($string)-5);$i正则表达式:
    &.{0,4}$
    应该完成这项任务。它将找到结尾前的最后0-4个字符,这些字符位于(包括)一个&字符之后

    $string = 'Hello World&foo';
    echo $string;
    $string = preg_replace('/&.{0,4}$/', '', $string);
    echo $string;
    

    正则表达式:
    &.{0,4}$
    应该可以完成这项任务。它将查找结尾前最后的0-4个字符,这些字符位于(包括)一个&字符之后

    $string = 'Hello World&foo';
    echo $string;
    $string = preg_replace('/&.{0,4}$/', '', $string);
    echo $string;
    

    如果要避免使用正则表达式,
    strpos
    可能会起到以下作用:

    $string='Hello world this day and tom&or&row';
    if (($pos = strpos ($string, '&', strlen($string) - 5)) !== false)
    {
        $string = substr($string,0, $pos);
    }
    

    示例。

    如果要避免使用正则表达式,
    strpos
    可能会起到以下作用:

    $string='Hello world this day and tom&or&row';
    if (($pos = strpos ($string, '&', strlen($string) - 5)) !== false)
    {
        $string = substr($string,0, $pos);
    }
    

    示例。

    /(?=.*.&.{5}$/
    如果不在其他地方,你可以使用explode你的意思是:$newstring=preg\u replace('/(?=.*.&.{5}$/');
    /(?=.*.&.{5}$/
    如果不在其他地方,你可以使用explode,你的意思是:$newstring=preg\u replace('/=(?=.*.-.-.-.},'/.},$5},$string/,$string);我认为它会离开&在这个例子中(但它应该删除&):
    $string='Hello world今明两天&'
    我刚刚测试了这个,它删除了最后一个&没有任何问题。我认为它会离开&在这个例子中(但它应该删除&):
    $string='Hello world今明两天&'
    我刚刚测试了这个,它删除了最后一个&没有任何问题。谢谢,似乎有效:谢谢,似乎有效:你是对的,我忘记了。但是strpos还有一个偏移参数。我会修复代码。你是对的,我忘记了。但是strpos也有一个偏移参数。我很抱歉我会修改代码的。