2010-02-03 110 views
100

如何修剪字符串的一部分,並使用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

回答

19

假設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

131

如果你明確靶向 「11223344」,然後使用str_replace

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

執行以下操作

$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

5

SUBSTR()是一個內置的PHP函數返回一個字符串的一部分。 函數substr()將採用一個字符串作爲輸入,這是您希望修剪字符串的索引表單。而可選參數是子串的長度。你可以看到正確的文檔和示例代碼上http://php.net/manual/en/function.substr.php

注:指數字符串以0

78

開始可以使用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

5

當你需要基於規則的匹配,就需要使用正則表達式

$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

-1

如果你想從給定的字符串中刪除部分字符串 這可能會幫助你

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

0

你也可以這樣做,如果你喜歡其他方式......

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" 
0

動態,如果你想從一個字符串的固定指數(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 

它的一個用法是執行你知道它的結構的命令。