2012-04-06 52 views
1

嘿傢伙我使用霍基的plugin。它像嘰嘰喳喳,你可以指出某人。我在輸出時遇到了問題。此方法:jquery @mention使輸出鏈接

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(/[[email protected]#\$%\^&\*\(\)\+=-\[\]\\';,\.\/\{\}\|":<>\?~_]/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:peterwateber]//格式是@ [] 。

我使用PHP作爲我的後端。我的問題是我想將輸出轉換爲

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

我在這裏有與代碼的一個大問題,因爲我不擅長的正則表達式。我想出了代碼:

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

    $string = "@[1231:peterwateber] [email protected][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,才能夠得到輸出我有這樣的形式:

<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"

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\#$%&~/.\-;:=,[email protected]\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret); 
    $ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,[email protected]\[\]+]*)#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:\\[email protected]\\3\">\\[email protected]\\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; 
} 

一個url現在的問題是,我不知道如何轉換@ 1234:peterwateber]至**<a href="www.something.com/profile?pid=123">peterwateber</a>**以及如何排除錨標記用htmlspecialchars

回答

0

正則表達式取1231 - peterwateber和88 - 是喜是

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\#$%&~/.\-;:=,[email protected]\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret); 
    $ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,[email protected]\[\]+]*)#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:\\[email protected]\\3\">\\[email protected]\\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"輸入代碼在這裏

+0

感謝隊友!但它給了我這個輸出: Array([0] => Array([0] => 1 [1] => 2 [2] => 3 [3] => 1 [4] => 5] => p [6] => e [7] => t [8] => e [9] => r [10] => w [11] => a [12] => t [13] => e [14] => b [15] => e [16] => r [17] => s [18] => d [19] => f [20] => s [21] => d [22] => f [23] => s [24] => d [25] => f [26] => s [27] => d [28] => f [29] => 30] => d [31] => f [32] => s [33] => d [34] => f [35] => 8 [36] => 8 [37] => => h [39] => i [40] => s [41] => d [42] => d [43] => s [44] => f)) – 2012-04-06 11:43:37

+0

WOOW! +1對不起!!!我忘了'@'標誌。布拉沃! – 2012-04-06 11:45:56

+0

不理解關於「從htmlspecialchars排除錨標籤」的部分 - 它是什麼意思?給我們你的輸入字符串和你要排除的東西 – 2012-04-06 11:48:13