2016-11-20 83 views
0

我試圖匹配^Description^*http://google.com*並將其轉換爲URL。它與JS一起工作,但我不知道如何將它實現到PHP Array中。 我的JS是這樣的:一個數組字符串中的多個匹配

var that = $(this); 
    var vid = that.html().match(/\^(.*)\^/gm); 
    var vid2 = that.html().match(/\*(.*)\*/gm); 
    var vid2 = jQuery.trim(vid2).slice(1,-1); 
    var vid1 = jQuery.trim(vid).slice(1,-1); 
    that.html(function(i, h) { 
     return h.replace(/\^(.*)\^\*(.*)\*/gm, '<a target="_blank" href="'+vid2+'">'+vid1+'</a>'); 
    }); 

我的PHP數組:

$find = array('/\*\*([^*]+)\*\*/', '/@(\\w+)/'); 
$replace = array('<span style="font-weight:bold">$1</span>', '<a href=/profile/$1>@$1</a>'); 
$result = preg_replace($find, $replace, $comment_text); 

回答

0

我與做:)

$find = array('/\*\*([^*]+)\*\*/', '/@(\\w+)/', '/\^(.*)\^\*(.*)\*/'); 
$replace = array('<span style="font-weight:bold">$1</span>', '<a href=/profile/$1>@$1</a>','<a target="_blank" href="$2">$1</a>'); 
$result = preg_replace($find, $replace, $comment_text); 
0

在PHP你匹配正李毅華^Description^*http://google.com*可以使用/ s修飾符(如here所述)而不是正則表達式中的^。我測試了代碼here

<?php 
$find = array('/^Description.*\*(.*)\*/s'); 
$replace = array('<a href="$1">$1</a>'); 
$comment_text = 'Description 
*http://google.de*'; 
$result = preg_replace($find, $replace, $comment_text); 
echo $result; 
相關問題