Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在文本中解析Instagram用户名并将其替换为链接_Php_Regex - Fatal编程技术网

Php 在文本中解析Instagram用户名并将其替换为链接

Php 在文本中解析Instagram用户名并将其替换为链接,php,regex,Php,Regex,因此,根据这里的一些其他帖子,我能够使用以下方法从文本字符串解析hashtag: preg_replace('/(?:^|\s)#(\w+)/', ' <a href="https://instagram.com/tags/$1">$1</a>', $text); 应返回: @test @insta.usernames @regular.expressions 非常感谢您的帮助 如果用户名不能以点结尾,并且不能有连续点或只有点,则可以使用: (?:

因此,根据这里的一些其他帖子,我能够使用以下方法从文本字符串解析hashtag:

preg_replace('/(?:^|\s)#(\w+)/', ' <a href="https://instagram.com/tags/$1">$1</a>', $text);
应返回:

@test
@insta.usernames
@regular.expressions

非常感谢您的帮助

如果用户名不能以点结尾,并且不能有连续点或只有点,则可以使用:

(?:^|\s)[@#](\w+(?:\.\w+)*)
注意在您尝试的模式中,
(?:^\s)@(\w+
组1值包含
@

|


另一个选项可以是(包括@或#)

(?
|


在替换中,使用完全匹配的
$0

如果用户名不能以点结尾,并且不能有连续点或只有点,则可以使用:

(?:^|\s)[@#](\w+(?:\.\w+)*)
注意在您尝试的模式中,
(?:^\s)@(\w+
组1值包含
@

|


另一个选项可以是(包括@或#)

(?
|


在替换中使用完全匹配的
$0

如果在末尾使用单词边界,我认为这达到了您想要的效果

[@#][\w.]+\b

这回答了
应该返回的问题

对于PHP替换,您需要捕获用户名位:

echo preg_replace('/[@#]([\w.]+)\b/', '<a href="https://instagram.com/tags/$1">$1</a>', 'Hi this is a @test to parse @insta.usernames using @regular.expressions.');
echo preg#u replace('/[@#]([\w.]+)\b/“,'',您好,这是一个使用@regular.expressions解析@insta.usernames的@test');

如果你在结尾使用单词边界,我认为这就达到了你想要的效果

[@#][\w.]+\b

这回答了
应该返回的问题

对于PHP替换,您需要捕获用户名位:

echo preg_replace('/[@#]([\w.]+)\b/', '<a href="https://instagram.com/tags/$1">$1</a>', 'Hi this is a @test to parse @insta.usernames using @regular.expressions.');
echo preg#u replace('/[@#]([\w.]+)\b/“,'',您好,这是一个使用@regular.expressions解析@insta.usernames的@test');

echo preg_replace('/[@#]([\w.]+)\b/', '<a href="https://instagram.com/tags/$1">$1</a>', 'Hi this is a @test to parse @insta.usernames using @regular.expressions.');