[英]Extracting leading spaces from the rest of the string
將字符串拆分為其前導空格和其余空格的最快方法是什么?
····sth
應該變成array("····", "sth")
和·sth·
- array("·", "sth·")
* ·
= 空格
$result = preg_split('/^(\s*)/', ' test ', -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
產出
array(2) {
[0]=>
string(2) " "
[1]=>
string(5) "test "
}
這是一個更簡單的方法:
$result = preg_split('/\b/', ' sth', 2);
它會輸出:
array (size=2)
0 => string ' ' (length=3)
1 => string 'sth' (length=3)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.