【问题标题】:How can I remove part of a string in PHP? [closed]如何在 PHP 中删除部分字符串? [关闭]
【发布时间】:2011-01-12 15:01:15
【问题描述】:

如何删除部分字符串?

示例字符串:"REGISTER 11223344 here"

如何从上面的示例字符串中删除"11223344"

【问题讨论】:

  • 您的问题不清楚/模棱两可。您正在使用术语remove,但您的意思似乎很可能是extract,以便可以将目标字符串存储在数据库中。
  • @Makyen 我非常有信心您已经破坏了这个问题以使许多答案正确。想一想:OP 为什么要在数据库中保存REGISTER here
  • @mickmackusa remove 可以与带走同义,即从字符串中取出 11223344,我在这里看不到问题。在最坏的情况下,它是模棱两可的,它肯定不会被“破坏”
  • 阅读预破坏版本。 @Nick stackoverflow.com/posts/2192170/revisions 真相会改变你的想法。我很高兴看到这个问题以 Unclear 的形式结束。
  • @mickmackusa 我做到了......我没明白你的意思。如果您对它所写的方式有这么大的问题,为什么不自己解决它

标签: php string


【解决方案1】:

下面的sn-p会打印“REGISTER here”

$string = "REGISTER 11223344 here";

$result = preg_replace(
   array('/(\d+)/'),
   array(''),
   $string
);

print_r($result);

preg_replace() API 用法如下。

$result = preg_replace(
    array('/pattern1/', '/pattern2/'),
    array('replace1', 'replace2'),
    $input_string
);

【讨论】:

  • 在这种情况下绝对没有理由将数组传递给preg_replace()。使用捕获组也没有任何好处。
  • 我同意。添加数组只是为了方便。它可以忽略不计。它不会造成伤害。添加了用于排除数字的捕获组。这是按照绘制的问题工作的。并且可以根据个人进行进一步的修改和微调。
  • 我的意思是,您的 sn-p 中有不必要的包含,包含没有意义。
【解决方案2】:

当需要基于规则的匹配时,需要使用正则表达式:

$string = "REGISTER 11223344 here";
preg_match("/(\d+)/", $string, $match);
$number = $match[1];

这将匹配第一组数字,因此如果您需要更具体,请尝试:

$string = "REGISTER 11223344 here";
preg_match("/REGISTER (\d+) here/", $string, $match);
$number = $match[1];

【讨论】:

  • preg_replace() 会容易得多,不是吗? us2.php.net/manual/en/function.preg-replace.php
  • @ElijahLynn replace 要求您删除东西,未来的变化或噪音会破坏。通过 _replace 执行此操作,但 _match 将继续工作。
  • @TravisO 好吧,是的,但不是。这两个在实现方面几乎是相同的功能,并且未来更改影响其中任何一个的机会都是相同的。仅 _match 也有未来更改的相同可能性,使此使用无效并且 _replace 仍然可以正常工作。此外,如果可能,版本控制通常非常注意不破坏以前的功能。
  • 这里不需要捕获组。事实上,使用捕获组不必要地使$match 数组膨胀。删除括号,然后通过$match[0] 访问。
【解决方案3】:

substr() 是一个内置的 PHP 函数,它返回字符串的一部分。 函数 substr() 将一个字符串作为输入,您希望在其中修剪字符串的索引形式,可选参数是子字符串的长度。您可以在 substr 上查看正确的文档和示例代码。

注意:字符串的索引从 0 开始。

【讨论】:

  • 你如何建议 OP 应该使用你的一般提示来达到预期的结果?
  • 我觉得这个答案含糊不清,价值不高。
【解决方案4】:

假设 11223344 不是常数:

$string="REGISTER 11223344 here";
$s = explode(" ", $string);
unset($s[1]);
$s = implode(" ", $s);
print "$s\n";

【讨论】:

    【解决方案5】:

    如果您专门针对“11223344”,请使用str_replace

    // str_replace($search, $replace, $subject)
    echo str_replace("11223344", "","REGISTER 11223344 here");
    

    【讨论】:

    • str_replace($search,$replace,$subject)
    【解决方案6】:
    str_replace(find, replace, string, count)
    
    • 查找必填。指定要查找的值
    • 替换 必需。指定要替换 find 中的值的值
    • 字符串 必需。指定要搜索字符串
    • 计数 可选。一个计算替换次数的变量

    根据 OP 示例:

    $Example_string = "REGISTER 11223344 here";
    $Example_string_PART_REMOVED = str_replace('11223344', '', $Example_string);
    
    // will leave you with "REGISTER  here"
    
    // finally - clean up potential double spaces, beginning spaces or end spaces that may have resulted from removing the unwanted string
    $Example_string_COMPLETED = trim(str_replace('  ', ' ', $Example_string_PART_REMOVED));
    // trim() will remove any potential leading and trailing spaces - the additional 'str_replace()' will remove any potential double spaces
    
    // will leave you with "REGISTER here"
    

    【讨论】:

    • 你如何建议 OP 使用这个伪代码?
    【解决方案7】:

    如果您想从字符串的 (a) 个固定索引中删除 (a) 个部分,请使用此函数:

    /**
     * Removes index/indexes from a string, using a delimiter.
     * 
     * @param string $string
     * @param int|int[] $index An index, or a list of indexes to be removed from string.
     * @param string $delimiter 
     * @return string
     * @todo Note: For PHP versions lower than 7.0, remove scalar type hints (i.e. the
     * types before each argument) and the return type.
     */
    function removeFromString(string $string, $index, string $delimiter = " "): string
    {
        $stringParts = explode($delimiter, $string);
    
        // Remove indexes from string parts
        if (is_array($index)) {
            foreach ($index as $i) {
                unset($stringParts[(int)($i)]);
            }
        } else {
            unset($stringParts[(int)($index)]);
        }
    
        // Join all parts together and return it
        return implode($delimiter, $stringParts);
    }
    

    为了您的目的:

    remove_from_str("REGISTER 11223344 here", 1); // Output: REGISTER here
    

    它的一个用途是执行类似命令的字符串,你知道它们的结构。

    【讨论】:

    • Ghostdog 已经提出了一种爆炸-内爆技术,但我无法想象在这样一个基本的操作中会接受如此冗长的技术。
    【解决方案8】:

    可以使用str_replace(),其定义为:

    str_replace($search, $replace, $subject)
    

    所以你可以把代码写成:

    $subject = 'REGISTER 11223344 here' ;
    $search = '11223344' ;
    $trimmed = str_replace($search, '', $subject) ;
    echo $trimmed ;
    

    如果您需要通过正则表达式进行更好的匹配,您可以使用preg_replace()

    【讨论】:

    【解决方案9】:

    执行以下操作

    $string = 'REGISTER 11223344 here';
    
    $content = preg_replace('/REGISTER(.*)here/','',$string);
    

    这将返回“REGISTERhere”

    $string = 'REGISTER 11223344 here';
    
    $content = preg_replace('/REGISTER (.*) here/','',$string);
    

    这将返回“在此处注册”

    【讨论】:

    • 它不会返回“REGISTER__here”,并带有两个空格(将下划线视为空格,注释不允许我有两个后续空格)?因为“(.*)”前后有空格。
    • 这个答案至少 100% 错误。证明:3v4l.org/LDLEM为什么这个答案有 15 个 UV?
    猜你喜欢
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多