回答
假設11223344不恆定
$string="REGISTER 11223344 here";
$s = explode(" ",$string);
unset($s[1]);
$s = implode(" ",$s);
print "$s\n";
這將讓他'11223344',這是(我認爲)與他想要的相反。 – 2010-02-03 13:35:25
如何?他正在取消設置包含*「11223344」的數組索引。 – 2010-02-03 14:03:15
我在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);
這將返回 「在這裏註冊」
難道它不會返回「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()。
如果需要正則表達式匹配,還有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];
preg_replace()會更容易嗎? http://us2.php.net/manual/en/function.preg-replace.php – 2013-07-11 17:40:21
@ElijahLynn替換要求你刪除的東西,未來的變化或噪音會打破。通過_replace做這件事,但_match會繼續工作。 – TravisO 2013-07-16 13:27:52
如果你想從給定的字符串中刪除部分字符串 這可能會幫助你
chop($string,'part_of_string');
'chop'是'rtrim'的別名,它不會這樣做 - 請參閱http://php.net/rtrim。 – 2013-05-12 06:53:33
是的,這是行不通的。 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
它的一個用法是執行你知道它的結構的命令。
- 1. 如何刪除部分字符串?
- 2. 如何刪除部分字符串
- 3. Excel VBA-刪除部分字符串
- 4. 刪除部分字符串PHP
- 5. 使用sed刪除字符串部分
- 6. 刪除部分字符串使用sed
- 7. SQL刪除字符串的一部分
- 8. PHP刪除部分字符串
- 9. Ruby刪除部分字符串
- 10. 刪除字符串的特定部分
- 11. 刪除部分字符串與PHP
- 12. C#刪除字符串「Color [ColorName]」中的字符串部分「Color []」
- 13. 如何使用RegExp刪除大字符串中的一小部分字符串
- 14. 如何在actionscript中刪除部分字符串?
- 15. 如何使用正則表達式刪除部分字符串?
- 16. 如何在java中刪除部分字符串?
- 17. 如何刪除字符串的已定義部分?
- 18. 如何在php中使用preg刪除一部分字符串?
- 19. 如何刪除字符串的一部分?
- 20. 如何刪除c中字符串的最後部分
- 21. 如何使用Ruby刪除部分CSV字符串?
- 22. 如何在JavaScript中的「:」之前刪除部分字符串?
- 23. 如何刪除mysql中的部分字符串?
- 24. 如何從使用JQuery的URL中刪除部分字符串?
- 25. 如何在php中刪除部分字符串?
- 26. 如何刪除字符串中特定部分的雙引號?
- 27. 如何刪除字符串的最後部分?
- 28. 如何刪除我的部分字符串?
- 29. 如何刪除B中字符串的第一部分
- 30. 如何刪除字符串?
這是哪種編程語言? – 2010-02-03 13:32:53
在帖子中說PHP。 – 2010-02-03 13:33:34
@siddharth - 你想要什麼輸出? – 2010-02-03 13:34:58