1
我需要幫助檢測字符串包含的標點符號在其之前或之後或行尾沒有空格。PHP正則表達式用於測試字符串中沒有至少有一個空格的標點符號
這是我很難言傳,所以我會用例嘗試:
這應該通過測試:
hello, my friend
hello ,my friend
hello , my friend
hello my friend
hello my friend.
這將失敗測試:
hello,my friend
hello, my friend,have a nice day
的功能我希望是這樣簡單:
function punctest($str)
{
if (preg_match("[:punct:]",$str))
{
if (SOME_REGEX_GOES_HERE)
{
$okay = 1; // punctuation found, but has a space before OR after it
}
else
{
$okay = 0; // punctuation found, not no space found either side
}
}
else
{
$okay = 1; // no punct found
}
}