2016-11-20 11 views
0

Ich versuche, ^Description^*http://google.com* zu entsprechen und in URL zu konvertieren. Es funktioniert mit JS, aber ich weiß nicht, wie dies in PHP Array implementiert wird. Mein JS sieht wie folgt aus:Mehrere Übereinstimmungen in einem Array-String

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

Und mein PHP Array:

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

Antwort

0

Ich habe damit fertig :)

$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

die regelmäßige experession ^Description^*http://google.com* in PHP entsprechen, die Sie könnte den Modifikator/s (wie beschrieben here) anstelle der^in der Mitte der Regex verwenden. Ich habe den Code here getestet.

<?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; 
Verwandte Themen