2016-01-13 22 views
0

我怎麼會去此字符串從PHP刪除

$text = " 
Hand #1 

text Hand #146822299460: 

Hand #2 

text Hand #146822236378: 


Hand #3 

text Hand #346822217642: 

Hand #10"; 

意志字符串刪除只是「手#X」精確匹配和字符串任意數量有可能成爲這樣的:

text Hand #146822299460: 



text Hand #146822236378: 




text Hand #346822217642: 

IM writeing這是因爲計算器說的這contans到太多的代碼

+0

@andrew認爲我的相反想要它做我需要刪除手#1手#2手#3和手#10 – gus

+0

@ chris85我編輯的帖子來顯示我想要的輸出 – gus

+0

@gus回答。 – chris85

回答

1

這應該工作:

$text = " 
Hand #1 

text Hand #146822299460: 

Hand #2 

text Hand #146822236378: 


Hand #3 

text Hand #346822217642: 

Hand #10"; 
echo preg_replace('/^Hand\h+#\d+\s*$/m', '', $text); 

輸出:

文字手#146822299460:

文字手#146822236378:


文字手#346822217642:

PHP演示:https://eval.in/501402
Regex101演示:https://regex101.com/r/dS7rW7/1

-1

使用preg_replace

echo preg_replace("/^(Hand\s#\d+)/", "", $text); 

輸出:

text : 



text : 




text : 

假設你只是想去掉其中「手#X」是在字符串開頭的線路,然後利用

echo preg_replace("/^(Hand\s#\d+)/m", "", $text); 
+0

我需要文本手#146822299460:顯示 – gus

+0

警告:preg_replace():沒有結尾分隔符'^'找到 – gus

+0

它根本不會改變文本 – gus