2016-01-22 28 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+$/結束製表:在一端或多個表格