PHP中自动将单词转换为链接


automatic convert word to link in PHP

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

例如,假设我们有一个文本"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?!如何?

谢谢。

这很容易。

<?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?!

这段代码是关于:如果某个短语中有一个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;
    }

为了更好地澄清问题:

我有一个HTML代码,其中有一些标记。我想要一些单词,转换成一些链接。但如果是另一个链接,则不会转换。请参阅下面的高级示例,了解我们想要链接到google:的特殊单词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];
    }