PHP从字符串中删除完全匹配和任何数字

时间:2016-01-13 00:28:41

标签: php regex

我将如何去除#34手#X"从这个字符串

$text = " 
Hand #1

text Hand #146822299460:

Hand #2

text Hand #146822236378: 


Hand #3

text Hand #346822217642:

Hand #10";

will string必须成为这个:

text Hand #146822299460:



text Hand #146822236378: 




text Hand #346822217642:
我正在写这篇文章,因为stackoverflow说它代表了很多代码

2 个答案:

答案 0 :(得分: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);

输出:

  

text Hand#146822299460:   
  
  文字手#146822236378:   
  
  
  文字手#346822217642:   

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

答案 1 :(得分:-1)

使用preg_replace

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

输出:

text :



text : 




text :

假设你只想删除"手#X"在字符串的开头,然后使用

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