如何删除部分字符串?

时间:2010-02-03 13:31:21

标签: php string

如何修剪字符串的一部分,并使用PHP将其保存在MySQL的特定字符串中?

示例字符串:"REGISTER 11223344 here"

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

11 个答案:

答案 0 :(得分:177)

如果您专门定位“11223344”,请使用str_replace

echo str_replace("11223344", "", "REGISTER 11223344 here");

答案 1 :(得分:96)

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

str_replace($search, $replace, $subject)

所以你可以把代码写成:

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

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

答案 2 :(得分:21)

假设11223344不是常数

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

答案 3 :(得分:15)

执行以下操作

$string = 'REGISTER 11223344 here';

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

这将返回“REGISTERhere”

$string = 'REGISTER 11223344 here';

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

这将返回“在此注册”

答案 4 :(得分:6)

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

$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];

答案 5 :(得分:5)

substr()是一个内置的php函数,它返回一个字符串的一部分。 函数substr()将把一个字符串作为输入,索引表单是你想要修剪字符串的地方。可选参数是子字符串的长度。您可以在http://php.net/manual/en/function.substr.php

上查看相应的文档和示例代码

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

答案 6 :(得分:3)

str_replace(find, replace, string, count)
  • 查找必填。指定要查找
  • 的值
  • 替换必填。在查找
  • 中指定要替换的值
  • 字符串是必需的。指定要搜索
  • 字符串
  • 计数(可选)。计算替换次数的变量

答案 7 :(得分:1)

动态地,如果要从(a)字符串的固定索引中删除(a)部分,请使用此函数:

/*
 * Remove index/indexes from a string, delimited by a space (or something else).
 * First, divides the string by delimiter and holds divided parts as an array.
 * If $index is an array, it removes all indexes of the divided string;
 * otherwise (i.e. has an int value), it removes just that index of the string.
 * At last, it joins all string parts together and return it as a string.
 * Note: If you want to use this function in PHP5, remove scalar type hints
 * (i.e. keywords before function arguments).
 * Note: For PHP versions lower than 7.0, remove scalar type hints (i.e. the
 * types before each argument) and the return type.
 */
function remove_from_str(string $str, $index, string $delimiter = " "): string
{
    // String parts
    $strParts = explode($delimiter, $str);

    // Remove indexes from string parts
    if (is_array($index))
        foreach ($index as $i)
            $strParts[(int)$i] = null;
    else
        $strParts[(int)$index] = null;

    // Remove null values, to prevent duplicating delimiter
    $strParts = array_filter($strParts);

    // Join all parts together and return it
    return implode($delimiter, $strParts);
}

出于您的目的:

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

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

答案 8 :(得分:0)

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

$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
);

答案 9 :(得分:-1)

如果您喜欢其他方式,也可以这样做......

function str_substract($remove, $subject){
    $strpos = strpos($subject, $remove);
    return substr($subject, 0, $strpos) . substr($subject, $strpos + strlen($remove));
}

$str = "Hello, world. This is amazing";
print str_substract('rld', $str);

/* Outputs: */
"Hello, wo. This is amazing"

注意:这不适用于所有字符,例如unicode字符串

答案 10 :(得分:-3)

如果要从给定字符串中删除字符串的一部分 这可能对你有帮助

chop($string,'part_of_string');
相关问题