如何删除部分字符串?

问题描述:

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

例字符串:"REGISTER 11223344 here"

如何从示例串删除"11223344"

+0

这是哪种编程语言? – 2010-02-03 13:32:53

+23

在帖子中说PHP。 – 2010-02-03 13:33:34

+1

@siddharth - 你想要什么输出? – 2010-02-03 13:34:58

假设11223344不恒定

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

这将让他'11223344',这是(我认为)与他想要的相反。 – 2010-02-03 13:35:25

+1

如何?他正在取消设置包含*“11223344”的数组索引。 – 2010-02-03 14:03:15

+1

我在Dominic的评论之后做了调整。 – ghostdog74 2010-02-03 14:35:35

如果你明确靶向 “11223344”,然后使用str_replace

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

执行以下操作

$string = 'REGISTER 11223344 here'; 

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

这将返回“REGISTERhere”

$string = 'REGISTER 11223344 here'; 

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

这将返回 “在这里注册”

+3

难道它不会返回“REGISTER__here”,有两个空格(认为下划线为空格,评论不会让我有两个后续空格)?使用“(。*)”之前和之后的空格。 – TechNyquist 2014-11-26 07:56:00

SUBSTR()是一个内置的PHP函数返回一个字符串的一部分。 函数substr()将采用一个字符串作为输入,这是您希望修剪字符串的索引表单。而可选参数是子串的长度。你可以看到正确的文档和示例代码上http://php.net/manual/en/function.substr.php

注:指数字符串以0

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

str_replace($search, $replace, $subject) 

所以,你可以写代码:

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

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

+1

如果需要正则表达式匹配,还有preg_replace()http://us2.php.net/manual/en/function.preg-replace.php – 2013-07-11 17:34:42

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

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

preg_replace()会更容易吗? http://us2.php.net/manual/en/function.preg-replace.php – 2013-07-11 17:40:21

+0

@ElijahLynn替换要求你删除的东西,未来的变化或噪音会打破。通过_replace做这件事,但_match会继续工作。 – TravisO 2013-07-16 13:27:52

如果你想从给定的字符串中删除部分字符串 这可能会帮助你

chop($string,'part_of_string'); 
+4

'chop'是'rtrim'的别名,它不会这样做 - 请参阅http://php.net/rtrim。 – 2013-05-12 06:53:33

+0

是的,这是行不通的。 str_replace()可能是最干净的。 http://stackoverflow.com/a/2192329/292408 – 2013-07-11 17:32:53

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

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" 

动态,如果你想从一个字符串的固定指数(ES)删除部(S),使用此功能:

/* 
* 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). 
*/ 
function remove_from_str($index, string $str, 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(1, "REGISTER 11223344 here"); 
// Output: REGISTER here 

它的一个用法是执行你知道它的结构的命令。