jquery @提及使输出成为链接

问题描述 投票:0回答:1

大家好,我正在使用 Hawkee 的这个插件。它就像 Twitter,你可以在其中@提及某人。我的输出有问题。这个方法:

updateHidden: function() {
        var trigger = this.options.trigger;

        var contents = this.element.val();
        for(var key in this.id_map) {
            var regex = trigger+key;
            regex = regex.replace(/[!@#\$%\^&\*\(\)\+=-\[\]\\';,\.\/\{\}\|":<>\?~_]/g, '\\$&');
            regex = new RegExp(regex, "g");
            //contents = contents.replace(regex, trigger+'['+this.id_map[key]+']');
            //I changed the code above to:
            contents = contents.replace(regex, '@[' + this.id_map[key] +':' + key + ']');
        }
        $(this.options.hidden).val(contents);
    }

上面的代码将被输出到影响其的隐藏标签,其中

输出: @[123:peterwaterber] //格式为@[]。

我使用 PHP 作为后端。我的问题是我想将输出转换为

<a href="www.something.com/profile?pid=123">peterwateber</a>

我对这里的代码有一个大问题,因为我不擅长正则表达式。我已经想出了代码:

    //THIS CODE SHOULD GET 1234,peterwateber,88,hi.

    $string = "@[1231:peterwateber] sdfsdfsdfsdfsdfsdf@[88:hi]sddsf";
    preg_match_all("^\[(.*?)\]^",$string,$matches,PREG_PATTERN_ORDER);
    foreach ($matches[1] as $match) {
        echo $match.'<br/>'; //outputs 1231:peterwateber, 88:hi
    }

    preg_match_all("^\[([\w\d]+):(.*?)\]^",$string,$aw,PREG_PATTERN_ORDER);
    foreach ($aw[1] as $match) {
        echo $match.'<br/>'; //sad to say this code outputs the text '1231 and 88'
    }

更重要的是,为了能够获得输出,我有以下形式:

<form class="form-horizontal" data-post="request" method="post">
  <div class="control-group boxTextAreaHolder">
    <textarea placeholder="What are you thinking?" class="UITextarea" title="What are you thinking?" name="statuspost" id="statuspost" tag-status="this"></textarea>
    <input type="hidden" name="tags" id="tag-post" />
  </div>
</form>

提交后,输出将被处理到此函数。此函数不允许任何 htmlspecialchars 并检测类似

"http://stackoverflow.com"

的 url
private static function validate_text($text = '') {
    // This method is used internally as a FILTER_CALLBACK
    if (mb_strlen($text, 'utf8') < 1)
        return false;
    // Encode all html special characters (<, >, ", & .. etc) and convert
    //$str = nl2br(htmlspecialchars($text));
    $str = htmlspecialchars($text);
    // the new line characters to <br> tags:
    // Remove the new line characters that are left
    $str = str_replace(array(chr(10), chr(13)), '', $str);
    $text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $str);
    $ret = ' ' . $text;
    $ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
    $ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
    $ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
    //$ret = preg_replace("#^*@([)([0-9-])(])#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
    $ret = substr($ret, 1);
    return $ret;
}

现在的问题是我不知道如何将 @[1234:peterwateber] 转换为

**<a href="www.something.com/profile?pid=123">peterwateber</a>**
以及如何从 htmlspecialchars 中排除 锚标记

php javascript ajax json jquery-plugins
1个回答
-1
投票

取 1231 - peterwaterber 和 88 - hi 的正则表达式是

preg_match_all("#@\[(\w+)\:(\w+)\]#', $str);

这取决于您输入的字符串中有哪些字符。

\w
假设您只有“单词”字符(字母和数字)。

$hidden_input = '@[123:web]hello world!';

preg_match('#@\[(\w+)\:(\w+)\]\s*(.*)$#', $hidden_input, $m);

echo '<a href="'.m[1].'">'.$m[2].'</a>'.$m[3];



function validate_text($text = '') {
    // This method is used internally as a FILTER_CALLBACK
    if (mb_strlen($text, 'utf8') < 1)
        return false;
    // Encode all html special characters (<, >, ", & .. etc) and convert
    //$str = nl2br(htmlspecialchars($text));
    $str = htmlspecialchars($text);
    // the new line characters to <br> tags:
    // Remove the new line characters that are left
    $str = str_replace(array(chr(10), chr(13)), '', $str);
    $text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $str);
    $ret = ' ' . $text;
    $ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
    $ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
    $ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
    //$ret = preg_replace("#^*@([)([0-9-])(])#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
    $ret = substr($ret, 1);
    return $ret;
}

var_dump(validate_text('@[123:peterwateber] fsdfsdfdsf'));

给予

string(30) "@[123:peterwateber] fsdfsdfdsf"

© www.soinside.com 2019 - 2024. All rights reserved.