我想删除以space
和#
符号开头的文本末尾的所有单词。 不应删除句子中的URL或主题标签。删除句子末尾的#标签
示例文本:
hello world #dontremoveme foobar http://example.com/#dontremoveme #remove #removeme #removeüäüö
我试过,但它会删除所有的井号标签:
$tweet = "hello world #dontremoveme foobar http://example.com/#dontremoveme #remove #removeme #removeüäüö";
preg_match_all("/(#\w+)/", $tweet, $matches);
var_dump($matches);
我的想法是要检查每一个字开始在文本的结尾领先#
用前面有space
,直到不再是这种情况。 如何将其转换为正则表达式?
我尝试过了,得到这个错误:'警告:preg_match_all() function.preg-match-all]:未知修饰符'g'' PHP:'$ tweet =“hello world #dontremove我foobar http://example.com/#dontremoveme #remove #removeme#removeüäüö“; preg_match_all(“/(#\ S +)* $/g”,$ tweet,$ matches); var_dump($ matches);'我需要改变什么? – Tom
请尝试以下操作: '$ re =“/(#\\ S +)* $ /”; $ str =“hello world #dontremoveme foobar http://example.com/#dontremoveme #remove #removeme#removeüäüö”; preg_match_all($ re,$ str,$ matches);' 这是生成的代码,你可以在这里找到:https://regex101.com/r/eH4bJ2/1#code_0 – jonas
这可以工作,但'print_r $匹配)'输出2个数组 - 如何获得一个数组中的所有标签? – Tom