php 如何删除字符串的一部分?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2192170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:26:34  来源:igfitidea点击:

How to remove part of a string?

phpstring

提问by siddharth

How can I remove part of a string?

如何删除字符串的一部分?

Example string : "REGISTER 11223344 here"

示例字符串: "REGISTER 11223344 here"

How can I remove "11223344"from the above example string?

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

回答by Dominic Rodger

If you're specifically targetting "11223344", then use str_replace:

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

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

回答by dhorat

You can use str_replace(), which is defined as:

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

str_replace($search, $replace, $subject)

So you could write the code as:

所以你可以把代码写成:

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

If you need better matching via regular expressions you can use preg_replace().

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

回答by ghostdog74

assuming 11223344 is not constant

假设 11223344 不是常数

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

回答by Stnfordly

str_replace(find, replace, string, count)
  • findRequired. Specifies the value to find
  • replaceRequired. Specifies the value to replacethe value in find
  • stringRequired. Specifies the stringto be searched
  • countOptional. A variable that counts the number of replacements
  • 查找必需。指定要查找的值
  • 替换必需。指定要替换find 中的值的值
  • 字符串必需。指定要搜索字符串
  • 计数可选。计算替换次数的变量

As per OP example:

根据 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"

回答by TravisO

When you need rule based matching, you need to use regex

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

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

That will match the first set of numbers, so if you need to be more specific try:

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

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

回答by Umair Jabbar

substr() is a built-in php function which returns part of a string. The function substr() will take a string as input, the index form where you want the string to be trimmed. and an optional parameter is the length of the substring. You can see proper documentation and example code on http://php.net/manual/en/function.substr.php

substr() 是一个内置的 php 函数,它返回字符串的一部分。函数 substr() 将接受一个字符串作为输入,即您想要修剪字符串的索引形式。可选参数是子串的长度。您可以在http://php.net/manual/en/function.substr.php上看到正确的文档和示例代码

NOTE : index for a string starts with 0.

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

回答by MAChitgarha

Dynamically, if you want to remove (a) part(s) from (a) fixed index(es) of a string, use this function:

动态地,如果要从字符串的 (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);
}

For your purpose:

为了您的目的:

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

One of its usages is to execute command-like strings, which you know their structures.

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

回答by arango_86

The following snippet will print "REGISTER here"

以下代码段将打印“在此处注册”

$string = "REGISTER 11223344 here";

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

print_r($result);

The preg_replace() API usage us as given below.

preg_replace() API 用法如下。

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

回答by Elitmiar

Do the following

请执行下列操作

$string = 'REGISTER 11223344 here';

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

This would return "REGISTERhere"

这将返回“REGISTERhere”

or

或者

$string = 'REGISTER 11223344 here';

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

This would return "REGISTER here"

这将返回“在这里注册”