2016-01-22 44 views
2

如何检查字符串末尾是否有制表符?哪种模式有效?我试过/\+t$/,但我得到了“字符串末尾没有任何选项卡”,尽管最后有一个制表符。preg_match如果制表符在字符串末尾

<?php 
$text = "Come to the dark side. We have cookies "; 
$pattern = "/\+t$/"; 

if(preg_match($pattern , $text)) 
{ 
    echo "There is a tab at the end of the string"; 
} 
else 
{ 
    echo "There is NO tab at the end of the string"; 
} 
?> 
+1

'$模式=“/ \ t $ /“'? - 在使用代码之前,我使用这个站点测试了我的PHP正则表达式。 https://regex101.com/ – Scott

+0

感谢这工作。我甚至在网站上测试过你的代码,我刚刚意识到它没有工作,因为网站代码模拟器不会重新调整标签页.-未来我将使用你的网站,谢谢! – Black

回答

2

你的正则表达式/\+t$/一个+符号后跟字符t在字符串的结尾相匹配。

我想你想:

  • /\t$/:在字符串
  • /\t+$/结束制表:在一端或多个表格