Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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 preg replace_在post中的回调提及和哈希标记_Php_Regex_Preg Replace_Preg Replace Callback - Fatal编程技术网

Php preg replace_在post中的回调提及和哈希标记

Php preg replace_在post中的回调提及和哈希标记,php,regex,preg-replace,preg-replace-callback,Php,Regex,Preg Replace,Preg Replace Callback,假设我有一条短信 $text = '@stackguy @flowguy I need to learn more advanced php #ilovephp'; 我想用这两个锚标签分别替换@StackGuy和@flowguy。这也适用于文本字符串中任意数量的@ <a href="url/stackguy">@stackguy</a> <a href="url/flowguy">@flowguy</a> 我还想用 <a href="

假设我有一条短信

$text = '@stackguy @flowguy I need to learn more advanced php #ilovephp';
我想用这两个锚标签分别替换@StackGuy和@flowguy。这也适用于文本字符串中任意数量的@

<a href="url/stackguy">@stackguy</a>
<a href="url/flowguy">@flowguy</a>

我还想用

<a href="search/ilovephp">#ilovephp</a>

它也应该适用于多个#。我猜大概是这样

preg_replace_callback('regex',
            create_function('$matches', '
                switch ($matches[1]) {
                    case "@":
                        return "<a href=url.$matches[2]'/'>" . $matches[2] . "</a>";
                    case "#":
                        return "<a>" . $matches[2] . "</a>";
                }
        '), $var);
preg\u replace\u回调('regex'),
创建函数(“$matches”,”
开关($matches[1]){
案例“@”:
返回“”;
案例“#”:
返回“$matches[2]”;
}
),$var);
正则表达式会是什么样子? 我的函数是要匹配所有需要的还是需要添加foreach循环?
谢谢。

如果您不想匹配前面有@或#的文本,请使用以下命令:
/([@#])(\S+/

查看此演示:

注意:当它位于标签内时,它将匹配相同的内容。如果您不想这样做,您需要的不仅仅是正则表达式。

preg\u replace()
preg\u replace\u callback()
默认情况下,这两种方法都会查找所有可能的匹配项,因此您不需要循环

考虑到这些值还没有被锚包住,我将使用

preg_replace('~@([^\s#@]+)~','<a href="url/\1">$0</a>',
    preg_replace('~#([^\s#@]+)~','<a href="search/\1">$0</a>',$text)
);
preg_replace(“~@([^\s#@]+)~”,“,
preg#u replace(“~#”([^\s#@]+)~”,“$text)
);

preg_replace_回调(“~([#@])([^\s#@]+)~”,创建_函数(“$m”,
“$dir=$m[1]==”#“?“搜索”:“url”。
“return”“;”
)美元文本);
接下来,preg_replace()就足够了

$patterns = array('/#(\w+)/', '/@(\w+)/');
$replacements = array('<a href="http://example.com/tag/$1">#$1</a>', 
                '<a href="http://example.com/user/$1">@$1</a>');

$html = preg_replace($patterns, $replacements, $text);
$patterns=array('/#(\w+/),'/@(\w+/);
$replacements=数组(“”,
'');
$html=preg_replace($patterns,$replacements,$text);

Sooo。。。问题是什么?你为什么“猜会是”什么?试试看,如果不行的话,我们就有办法了。您已经接近实际编写一些代码并进行了尝试。func(regex)最重要的部分是我不知道的。可能需要将其修改为
/([@#])(\S+/
)。Saffron需要匹配符号以确定将其包装在哪个锚定标记中。谢谢@lindrian和Gigawatt:函数是替换全部还是我需要一个循环?@SaffronHarris:
preg\u replace()
自动循环,您不必自己这样做。@lindrian:在标记中匹配相同的东西是什么意思?你的意思是如果我有相同的文本

,它会输出相同的结果吗?@SaffronHarris no,这不会影响正常情况
\S
表示“除任何类型的空格外的任何内容”,因此,如果您确定只想匹配,请使用字母数字字符,然后可以使用
\w
而不是
\S
[^\S#@]
。请注意,
\w
(单词字符)匹配a-z、a-z、0-9和下划线(389;)。您可以将其限制为所需的字符组,如
[a-zA-Z]
[a-zA-Z0-9]
等。无论如何,不要忘记加号以捕获尽可能多的字符,如Lindrian的建议。
$patterns = array('/#(\w+)/', '/@(\w+)/');
$replacements = array('<a href="http://example.com/tag/$1">#$1</a>', 
                '<a href="http://example.com/user/$1">@$1</a>');

$html = preg_replace($patterns, $replacements, $text);