automatic convert word to link in PHP

2019-05-30 23:17发布

I want write a simple code that convert special words to special link (for wiki plugin), if it's not a link!

For example suppose we have a text "Hello! How are you?!" and
we want convert are to <a href="url">are</a>, but if we have <a href="#"> Hello! How are you</a>?! or Hello! <a href="url">How are you?!</a> does not change. Because it's a link.

How can I do it in PHP?! With preg_replace?! How to?

Thanks.

3条回答
叛逆
2楼-- · 2019-05-30 23:23

this code is about : if there is a some URL in some phrase it will convert to a link

$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;
    }
查看更多
贪生不怕死
3楼-- · 2019-05-30 23:31

To better clarify the issue:

I have a HTML code that have some tags. I want some words in that, converted to some links. But if it is a another link does not convert. See below advanced example for special word you that we want linked to the google:

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

should be convert to:

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

Note that the first you changed, but that second you was not changed, because it's in the another <a> tag.


Answer:

Because of this work has issue with regular expression, this problem can solve without regular expression. Here a simple solution is given:

    $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];
    }
查看更多
该账号已被封号
4楼-- · 2019-05-30 23:38

It's easy.

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

Output:

First function output: Hello! How <a href="#">are</a> you?!

Second function output: Hello! how <a href="http://google.com">are</a> you?!


Alternative:

If you want to not detect <a> tags which were closed, you can use this alternative code:

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

This gives the output: Hello! how <a href="#"><a href="http://google.com">are</a> you?!

查看更多
登录 后发表回答