的jquery @mention使得输出链路 [英] jquery @mention making the output a link

查看:110
本文介绍了的jquery @mention使得输出链路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我使用此插件从Hawkee。它像Twitter在那里你可以@mention人。我在与输出的麻烦。这个方法:

Hey guys i'm using this plugin from Hawkee. Its like twitter where you can @mention someone. I'm having trouble with the output. This method:

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);
    }

以上将被输出到一个隐藏的标签影响code的,其中

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

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

I'm using PHP as my back end. My problem is I wanna convert the output to

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

我有一个很大的问题与codeS在这里,因为我不擅长的正则表达式。我已经拿出code:

I have a big problem with the codes here since I'm not good at RegEx. I have come up with the code:

    //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'
    }

Moreso,才能够得到输出我有这样的形式:

Moreso, to be able to get the output I have this form:

<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

When submitted the output will be processed to this function. This function does not allow any htmlspecialchars and detects a url like "http://stackoverflow.com"

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]到 **&LT; A HREF =www.something.com/profile?pid=123&GT ; peterwateber&LT; / A&GT; ** 以及如何排除的锚标签的htmlspecialchars

The problem now is that I don't know how to convert @[1234:peterwateber] to **<a href="www.something.com/profile?pid=123">peterwateber</a>** and how to exclude anchor tags from htmlspecialchars

推荐答案

普通EX pression采取1231 - peterwateber和88 - 是喜是

Regular expression to take 1231 - peterwateber and 88 - hi is

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

这要看你有没有在你输入的字符串是什么样的字符。 \ W 假设你只有字的字符(字母和数字)。

It depends on what kind of characters do you have in your input string. \w assumes you have only "word" characters (letters and digits).

$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'));

串(30)@ [123:peterwateber] fsdfsdfdsf在此输入code

gives string(30) "@[123:peterwateber] fsdfsdfdsf"enter code here

这篇关于的jquery @mention使得输出链路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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