Identify a given url and change href property with preg_match_all

Asked

Viewed 241 times

0

Personal opa,

I have a function that should identify a given url. If the url is identified, it should be involved with the tag <a> and its property href must be equal to the value of the url passed. In this case, I want only one url and your directories to be transformed into links, while other different url remain the same as they are.

Example: The url I want to link to whenever it appears in the text is: www.meusite.com.br

The problem I’m having is this: In my role, I’m not able to identify the url without http. What I need is to identify the url with or without the http protocol and have your href filled in correctly.

I need to identify the 3 cases below, but I can only identify the first.

http://www.meusite.com.br
www.meusite.com.br
meusite.com.br

Remember that urls other than this one should not be identified in the function filter. Ex: meusite2.com.br nay must be filtered

Follow my role with what I tried:

    function checkUrl($text){
        $pattern = "(https?)?://[a-z0-9./&?:=%-_]*";

        preg_match_all($pattern, $text, $matches);
        if( count($matches[0]) > 0 ){
            foreach($matches[0] as $match){
                $text = str_replace($match, "<a href='" . $match . "' target='_blank'>" . $match . "</a>", $text);
            }
        }
        return $text;
    }

$text = "Em http://www.meusite.com.br você encontra muitas dicas sobre o assunto. Na url www.meusite.com.br você encontra especialistas com seus artigos esclarecdores. E claro, meusite.com.br é o melhor site no assunto"; 

I believe my problem is only in the $Pattern variable, where I pass the pattern to be found.

I searched the forum here, but I couldn’t find exactly what I needed

Identify Urls and create links

The link above didn’t help, and I should do this in php.

  • With http is working properly?

  • Yes. My problem is when I remove http from url. It stops recognizing :(

  • I was just wondering 'cause in your code there’s https...

  • that part there says that both http and https can be used. In this case the use of s would be optional.

  • 1

    I drew, RE It’s not my strong...

  • kkkk or mine, so I’m asking for help kkk

  • hahaha, I think my answer should help...

Show 2 more comments

1 answer

0

You can treat before forever have http in the url as follows:

function checkUrl($text){

    //primeiro garantindo que todos vão estar iguais
    $find = ['http://www.meusite.com.br', 'https://www.meusite.com.br', 'www.meusite.com.br'];
    $replace = "meusite.com.br";
    $text = string_replace($find, $replace, $text);

    //depois será possível colocar o http sem duplicar, caso já tivesse antes
    $find = ['meusite.com.br'];
    $replace = "http://www.meusite.com.br";
    $text = string_replace($find, $replace, $text);

    //e o que já estiver funcionando vai tratar tudo agora...
    $pattern = "(https?)?://[a-z0-9./&?:=%-_]*";

    preg_match_all($pattern, $text, $matches);
    if( count($matches[0]) > 0 ){
        foreach($matches[0] as $match){
            $text = str_replace($match, "<a href='" . $match . "' target='_blank'>" . $match . "</a>", $text);
        }
    }
    return $text;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.