2015-11-01 57 views
1

我想编写一个简单的代码,将特殊字转换为特殊链接(对于wiki插件),如果它不是链接!自动转换字链接到PHP

例如,假设我们有一个文本"Hello! How are you?!"
我们要转换are<a href="url">are</a>如果我们有<a href="#"> Hello! How are you</a>?!Hello! <a href="url">How are you?!</a>不会改变。因为这是一个链接。

我该如何在PHP中做到这一点?用preg_replace?!如何?

谢谢。

回答

0

要更好地澄清问题:

我有一个HTML代码,有一些标签。我想要一些单词,转换成一些链接。但如果它是另一个链接不转换。请参阅下面先进的例子为我们想链接到谷歌特殊字you

This <a href="#">is a sample</a> text. Hello?! How are you?! <a href="#1">Are you ready</a>?!

应该转换为:

This <a href="#">is a sample</a> text. Hello?! How are <a href="http://www.google.com">you</a>?! <a href="#1">Are you ready</a> ?!

那第一个you已更改,但第二个you未更改,因为它位于另一个<a>标记中。


答:

因为这个工作有问题,正则表达式,这个问题可以不用正则表达式解决。这里给出一个简单的解决方案:

$data = 'Hello! This is a sample text.   <br/>'. 
    'Hello! This <a href="#1">is</a> a sample text. <br/>'. 
    'Hello! This <a href="#2">is a sample text.</a> <br/>'. 
    'Hello! <a href="#3">This is a sample</a> text. <br/>'. 
    '<a href="#4">Hello! This</a> is a sample text.'; 

    $from = " is "; 
    $to = '<a href="http://www.google.com" > '.$from.' </a>'; 

    echo $data; 
    $data = explode($from, $data); 
    echo "<br><br>"; 

    echo $data[0]; 
    $diff = 0; 
    for($i=1; $i<count($data); $i++){ 
     $n = substr_count($data[$i-1], '<a ') + substr_count($data[$i-1], '<A '); 
     $m = substr_count($data[$i-1], '</a>') + substr_count($data[$i-1], '</A>'); 

     $diff += $n-$m; 
     if($diff==0) 
      echo $to.$data[$i]; 
     else 
      echo $from.$data[$i]; 
    } 
2

这很容易。

<?php 

$string = "Hello! How <a href=\"#\">are</a> you?!"; 
$stringTwo = "Hello! how are you?!"; 

function turnTheWordIntoALink($string, $word, $link) { 
    if(isLink($string)) { 
     return $string; 
    } else { 
     $string = str_replace($word, "<a href=\"" . $link . "\">" . $word . "</a>", $string); 
     return $string; 
    } 
} 

function isLink($string) { 
    return preg_match("/(<a href=\".\">)/", $string); 
} 

echo turnTheWordIntoALink($string, 'are', 'http://google.com'); 
echo turnTheWordIntoALink($stringTwo, 'are', 'http://google.com'); 

输出:

第一函数输出:Hello! How <a href="#">are</a> you?!

第二函数输出:Hello! how <a href="http://google.com">are</a> you?!


备选:

如果你想不检测<a>标签,其被关闭,您可以使用此替代代码:

$stringThree = "Hello! how <a href=\"#\">are you?!"; 

function turnTheWordIntoALink($string, $word, $link) { 
    if(isLink($string)) { 
     return $string; 
    } else { 
     $string = str_replace($word, "<a href=\"" . $link . "\">" . $word . "</a>", $string); 
     return $string; 
    } 
} 

function isLink($string) { 
    return preg_match("/(<a href=\".\">)+(.)+(<\/a>)/", $string); 
} 

echo turnTheWordIntoALink($stringThree, 'are', 'http://google.com') . "\n"; 

这使输出:Hello! how <a href="#"><a href="http://google.com">are</a> you?!

+0

Thnaks。 很有用。 – shozdeh

+0

但有一个问题, 假设'Hello!你好吗?'。对于这种情况不起作用。请注意,替换是“单词”,但转换为HTML代码。 – shozdeh

+0

@shozdeh你是什么意思? –

0

这段代码大概是:如果有一些URL在一些词组,将转换为一个链接

$word = 'hello how are you google.com, wish you good time'; 
$prg = "/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; 
    if(preg_match($prg, $word, $url)) 
    { 
     echo preg_replace($prg, "<a href=http://$url[0]>{$url[0]}</a>", $word); 
    } 
    else 
    { 
     echo $word; 
    }