JHtml-"email.cloak"在joomla中使用preg_replace()时无法识别电子邮件地址 [英] JHtml-"email.cloak" doesn't recognize email address when using preg_replace() in joomla

查看:93
本文介绍了JHtml-"email.cloak"在joomla中使用preg_replace()时无法识别电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用JHtml::_("email.cloak", "some@email.com")用php替换php模块中文本中的电子邮件地址,以便将其隐藏在joomla 3.9.2网页上.

I try to substitute an email-address within a text in a module with php using JHtml::_("email.cloak", "some@email.com"), in order to cloak it on a joomla 3.9.2 webpage.

使用以下示例代码:

<div>
<?php
    $text = "This is a text with some@email.com an e-mail address in it";
    $test_text_email = "some@email.com";
    echo JHtml::_("email.cloak", $test_text_email)."<br>";
    echo preg_replace('/([a-zA-Z0-9_\-\.]*@\\S+\\.\\w+)/', JHtml::_("email.cloak", "$1"),$text)."<br>";
?></div>

Jhtml:_("email.cloak",...当只交出一个简单的字符串时效果很好(请参阅下面的$ test_text_email)当使用preg_replace在文本中搜索电子邮件地址时,可以正确找到电子邮件子字符串,但由于函数似乎无法再识别"@"符号,因此隐藏功能无法正常工作,结果如下:

Jhtml:_("email.cloak",... works great when just handing over a simple string (see $test_text_email below). When searching for an email address within a text using preg_replace, the email substring is found correctly, yet the cloaking doesn't work correctly as the function doesn't seem to recognize the "@" symbol anymore with the following result:

<span id="cloak7c9ea7a5340755f2f7e1d4f0c8b45675"><a href="mailto:some@email.com">some@email.com</a></span>
This is a text and <span id="cloak88a24a939973cdd7ec9f3d1fb591b7a5"><a href="mailto:some@email.com@">some@email.com@</a></span> an e-mail address 

生成的javascript证明由preg_replace()找到的正确子字符串,但是没有发生任何伪装,仅在末尾添加了@符号(&#64).

The generated javascript proves the correct substring found by preg_replace(), but without any cloaking happening, just an @ symbol is added at the end (&#64).

<script type="text/javascript">
            document.getElementById('cloak3fd71e407852d6d7593b726a0f41ca38').innerHTML = '';
            var prefix = '&#109;a' + 'i&#108;' + '&#116;o';
            var path = 'hr' + 'ef' + '=';
            var addy3fd71e407852d6d7593b726a0f41ca38 = 'some@email.com' + '&#64;';
            addy3fd71e407852d6d7593b726a0f41ca38 = addy3fd71e407852d6d7593b726a0f41ca38 + '';
            var addy_text3fd71e407852d6d7593b726a0f41ca38 = 'some@email.com' + '&#64;' + '';document.getElementById('cloak3fd71e407852d6d7593b726a0f41ca38').innerHTML += '<a ' + path + '\'' + prefix + ':' + addy3fd71e407852d6d7593b726a0f41ca38 + '\'>'+addy_text3fd71e407852d6d7593b726a0f41ca38+'<\/a>';
    </script>

脚本中的正确行如下所示:

The correct line in the script is shown below:

    var addy1ef8a36d65e0cc59448f875761cc1465 = 's&#111;m&#101;' + '&#64;';addy1ef8a36d65e0cc59448f875761cc1465 = addy1ef8a36d65e0cc59448f875761cc1465 + '&#101;m&#97;&#105;l' + '&#46;' + 'c&#111;m';

有什么想法为什么要对字符串进行不同的处理? 预先感谢!

Any ideas why the strings are handled differently? Thanks in advance!

推荐答案

您不能对preg_replace()的替换参数执行操作.您需要致电preg_replace_callback().

You can't perform manipulations on the replacement parameter of preg_replace(). You will need to call preg_replace_callback().

代码:(演示)

$text = "This is a text with some@email.com an e-mail address in it";

echo preg_replace_callback(
        '~[\w.-]+@(?:[\w-]+\.)+\w+~',
        function($m) {
            return JHtml::_("email.cloak", $m[0]);
        },
        $text
     );

  • a-zA-Z0-9_更简单地浓缩为\w
  • .不需要在字符类中转义.
  • 如果
  • -位于字符类的开始或结尾,或者紧随字符类中的字符范围"(例如0-9-),则不需要转义.
  • 您的图案中不需要那些双斜杠
  • 在某些情况下
  • \S之后出现贪婪的+可能不是一个好主意
    • a-zA-Z0-9_ is more simply condensed to \w
    • . doesn't need to be escaped in a character class.
    • - doesn't need to be escaped if it is at the start or end of the character class or if it immediately follows a "range of characters" in the character class (e.g. 0-9-).
    • you don't need those double slashes in your pattern
    • a greedy + after \S may be a bad idea in some scenarios
    • 这篇关于JHtml-"email.cloak"在joomla中使用preg_replace()时无法识别电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆