在字符串中找到@username
的最佳方法是什麼?如何在字符串中查找@username?
起初我是根據@
找到的字符串進行爆炸,但是看起來每個實例都經歷了很多麻煩。
我可以用正則表達式來查找字符串中的所有@usernames
嗎?
在字符串中找到@username
的最佳方法是什麼?如何在字符串中查找@username?
起初我是根據@
找到的字符串進行爆炸,但是看起來每個實例都經歷了很多麻煩。
我可以用正則表達式來查找字符串中的所有@usernames
嗎?
你可以用strpos()代替
爲引用http://php.net/manual/en/function.strpos.php
$pos = strpos($mystring, $findme);
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
是當然的。正則表達式是正確的方式去:
if (preg_match_all('[email protected](.+)(?:\s|$)!U', $text, $matches))
$usernames = $matches[1];
else
$usernames = array(); // empty list, no users matched
在http://stackoverflow.com/questions/4424179/twitter-username-regex-validation – danielrsmith 2012-03-27 14:08:41