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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.