0
後的單詞我試圖刪除字符串中的用戶名。嘗試下面的代碼,但它的@preg_replace刪除@
$string = 'he @username die';
$string = preg_replace('/@.*/','',$string);
echo $string; // output: he
我所要的輸出是後刪除了一切:他死
感謝
後的單詞我試圖刪除字符串中的用戶名。嘗試下面的代碼,但它的@preg_replace刪除@
$string = 'he @username die';
$string = preg_replace('/@.*/','',$string);
echo $string; // output: he
我所要的輸出是後刪除了一切:他死
感謝
使用\S
這意味着什麼,是不是一個空格字符(相反的的\s
),而不是.
:
$string = 'he @username die';
$string = preg_replace('/@\S+/','',$string);
echo $string; // output: he die
您可能還希望刪除以下空間:
$string = 'he @username die';
$string = preg_replace('/@\S+\s*/','',$string);
echo $string; // output: he die
完美地工作!但我沒有看到第一個和第二個之間的任何區別。兩個輸出都是他死的。是因爲PHP版本嗎? –
@ira_:第一個「he」和「die」之間有2個空格,第二個只有1個空格。 – Toto