2016-11-20 109 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;