Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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 如何同时进行自动链接和htmlspecialchars转换?_Php_Regex - Fatal编程技术网

Php 如何同时进行自动链接和htmlspecialchars转换?

Php 如何同时进行自动链接和htmlspecialchars转换?,php,regex,Php,Regex,我想创建同时满足以下两个条件的代码 如果文本包含任何url,则部件将自动转换为url 此文本中的、'等由func1适当转换。所以标签不起作用 1和2工作不正常 我能猜出为什么会这样。可能,func1将等转换为&60&62;, 等等,所以func2中的regexp将它们解释为url的一部分 我能猜出原因,但我不知道现在该怎么办 我正在考虑在func2中的$url_模式中添加一些单词,以排除%nbsp;或60英镑;不被解释。然而,如何用regexp表达这一点对我来说也是一个很大的障碍 我在这个问题上

我想创建同时满足以下两个条件的代码

如果文本包含任何url,则部件将自动转换为url

此文本中的、'等由func1适当转换。所以标签不起作用

1和2工作不正常

我能猜出为什么会这样。可能,func1将等转换为&60&62;, 等等,所以func2中的regexp将它们解释为url的一部分

我能猜出原因,但我不知道现在该怎么办

我正在考虑在func2中的$url_模式中添加一些单词,以排除%nbsp;或60英镑;不被解释。然而,如何用regexp表达这一点对我来说也是一个很大的障碍

我在这个问题上花了很长时间,但它很难解决。请帮帮我

如果您有任何不理解的地方,请留下评论。

问题是func1转换了中的所有空格,因此当正则表达式查看结果时,它会看到中断-例如,在hello之后,下一个字符是&这是正则表达式中允许的

在进行HTML转义之前,应该先运行URL捕获正则表达式,然后再进行HTML转义


顺便说一句-请使用htmlspecialchars,而不是您自己的自定义函数-正如@tadman所指出的。这样做的一个主要优点是,htmlspecialchars不会转换空格,因此不会遇到您描述的问题,而且-将空格转换为非中断空格通常不是一个好主意。

我想您可以尝试一下。防范url中的实体

Php

具有额外换行间距的输出

hello universe! 

<a href='https://www.youtube.com/watch?v=test' target='_blank' class='temp_class'>
https://www.youtube.com/watch?v=test
</a>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#60;iframe&nbsp;src=&#39;

<a href='https://youtube.com/watch?v=good' target='_blank' class='temp_class'>
https://youtube.com/watch?v=good
</a>

&#39;&#62;&#60;/iframe&#62;&nbsp;hello&nbsp;world.

使用htmlspecialchars,不要编写自己的。你能为你的链接器提供一些更好的用例吗?你找过了吗?这是你想要的输出吗?你好,宇宙&60;iframe src=&39&39;&62;&60;/iframe&62;你好,世界。
 ( https? )                    # (1)
 : //
 (                             # (2 start)
      (?:
           (?!
                (?i)
                (?:
                     &
                     (?:
                          [a-z_:] [a-z\d_:.-]* 
                       |  (?:
                               \#
                               (?: [0-9]+ | x [0-9a-f]+ )
                          )
                     )
                  |  % [a-z_:] [a-z\d_:.-]* 
                )
                ;
           )
           [a-zA-Z0-9./?&=%_+-@~:#$] 
      )+
 )                             # (2 end)
<?php
    function func1($text) //function like htmlspecialchars
    {
        $text = str_replace("&", "&#38;", $text);
        $text = str_replace("\"", "&#34;", $text);
        $text = str_replace("'", "&#39;", $text);
        $text = str_replace("<", "&#60;", $text);
        $text = str_replace(">", "&#62;", $text);
        $text = str_replace(" ", "&nbsp;", $text);
        return $text;
    }
    function func2($text)
    {
        $text = func1($text);
        $url_pattern = "(http|https):\/\/((?:(?!(?i)(?:&(?:[a-z_:][a-z\d_:.-]*|(?:\#(?:[0-9]+|x[0-9a-f]+)))|%[a-z_:][a-z\d_:.-]*);)[a-zA-Z0-9.\/?&=%_+-@~:#$])+)";
        $text = preg_replace("/(".$url_pattern.")/i", "<a href='\\1' target='_blank' class='temp_class'>\\1</a>", $text);
        return $text;
    }
    $test_string ="hello universe! https://www.youtube.com/watch?v=test     <iframe src='https://youtube.com/watch?v=good'></iframe> hello world.";
    echo func2($test_string);
hello&nbsp;universe!&nbsp;

<a href='https://www.youtube.com/watch?v=test' target='_blank' class='temp_class'>
https://www.youtube.com/watch?v=test
</a>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#60;iframe&nbsp;src=&#39;

<a href='https://youtube.com/watch?v=good' target='_blank' class='temp_class'>
https://youtube.com/watch?v=good
</a>

&#39;&#62;&#60;/iframe&#62;&nbsp;hello&nbsp;world.