簡體   English   中英

從字符串的其余部分提取前導空格

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM