PHP从字符串中删除精确匹配和任何数字


PHP remove exact match and any number from string

如何从这个字符串中删除"hand#X"

$text = " 
Hand #1
text Hand #146822299460:
Hand #2
text Hand #146822236378: 

Hand #3
text Hand #346822217642:
Hand #10";

字符串必须变成这样:

text Hand #146822299460:

text Hand #146822236378: 


text Hand #346822217642:

我写这篇文章是因为stackoverflow说它包含了很多代码

这应该有效:

$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手牌编号146822299460:

text手牌编号146822236378:


text手号34682217642:

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

使用preg_replace

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

输出:

text :

text : 


text :

假设您只想删除字符串开头"hand#X"的行,则使用

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