[英]php regex to find words in string starting with # and not have any more repeated #
I'm looking for a regex to find words in string starting with # and not have any more repeated # 我正在寻找一个正则表达式来查找以#开头的字符串而不再重复#
Given: 鉴于:
Hi I'm #hany #هانى my Friend #john#john is ##here . good bye#all .
I want the final result as this: 我希望得到最终结果:
Hi I'm <a>#hany</a> <a>#هانى</a> my Friend #john#john is ##here . good bye#all .
I'm using this: 我正在使用这个:
echo preg_replace('/(?!\b)#(\\S+)/','<a>$0</a>',$string);
I want only words starting with #
and not have any more hashes like twitter hash-tags. 我只想要以
#
开头的单词而不再有像twitter hash-tags这样的哈希。
Try this pattern: 试试这种模式:
echo preg_replace('/(?<=\s)(#[^#\s]+)(?=\s)/', '<a>$0</a>' ,$string);
Output: 输出:
Hi I'm <a>#hany</a> <a>#هانى</a> my Friend #john#john is ##here . good bye#all .
Try with this: 试试这个:
echo preg_replace('~(?<=\s|^)#[^\s#]++~um', '<a>$0</a>', $string);
Explanations: 说明:
~ # pattern delimiter (instead of /, but it's the same)
(?<= # open a lookbehind (means "preceded by")
\s | ^ # a white character (space, tab, newline...) or the begining of the line
) # close the lookbehind
# # literal #
[^\s#]++ # all that is not a # or a white character, one or more times (possessive)
~ # delimiter
um # u for unicode string, m for multiline mode
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.