2 回答

TA貢獻2021條經驗 獲得超8個贊
調整從PHP 中的字符串中提取 Twitter 主題標簽的答案以捕獲用戶標簽,這在同一個正則表達式中使用兩者來生成 2 個列表。過濾器 ( array_filter()) 用于整理數組(當一個包含用戶標簽時,另一個將為空)...
$string = '@abc1 xxx,@abvc321, #NewAge , #CurrentNews';
preg_match_all("/(#\\w+)|(@\\w+)/", $string, $matches);
$topicTag = array_filter($matches[1]);
$userTag = array_filter($matches[2]);
print_r($userTag);
print_r($topicTag);
給...
Array
(
[0] => @abc1
[1] => @abvc321
)
Array
(
[2] => #NewAge
[3] => #CurrentNews
)

TA貢獻1875條經驗 獲得超5個贊
親愛的Nigel Ren,我對您的解決方案幾乎沒有改動。因為$userTag是對象格式。所以它的數據類型會改變。這不會影響foreach。但我嘗試將兩者都制成數組格式。
$string = $req->user_tags;
preg_match_all("/(#\\w+)|(@\\w+)/", $string, $matches);
$topicTag = array_values(array_filter($matches[1]));
$userTag = array_values(array_filter($matches[2]));
在此之后我的結果
[
[
"@pankaj",
"@pawan"
],
[
"#tag",
"#tag2",
"#tage3"
]
]
- 2 回答
- 0 關注
- 128 瀏覽
添加回答
舉報