我必須得到一個在字符串中包含“#”的值。Ex:$string = '#1234 Lorem ipsum is placeholder text commonly used in the graphic, #rm1345 print, and publishing industries for previewing layouts and visual mockups';Expected results: #1234 and #rm1345我應(yīng)該如何從 $string 中獲得上述結(jié)果?我嘗試了以下方法,但對我沒有幫助。$contents = str_word_count($string, 2);echo 'contents: <pre>';print_r($contents);echo '</pre>';請幫幫我。
1 回答

幕布斯6054654
TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個贊
這是您的正則表達(dá)式和生成的字符串
$string = '#1234 Lorem ipsum is placeholder text commonly used in the graphic, #rm1345 print, and publishing industries for previewing layouts and visual mockups';
$temp = preg_match_all('/(?<!\w)#\w+/', $string, $matches);
echo implode(' and ', $matches[0]);
輸出:
#1234 and #rm1345
工作演示。
正則表達(dá)式測試鏈接。(preg_match_all 匹配所有 # 附加單詞)。
來源鏈接。
- 1 回答
- 0 關(guān)注
- 309 瀏覽
添加回答
舉報(bào)
0/150
提交
取消