Rotate text and replace first link

Asked

Viewed 165 times

2

I have a variable named $text where text is stored that the user writes.

`$text = "Hahahha that cool this site, I was able to answer all my questions there /"

`

What I need?

I have a function named makelink($url); that mounts a preview of the content of the link. That is, I need to create a function that will catch the FIRST text link and play it in this function, the function will create a preview of the link and will return an output with the result in HTML, after playing the link in the function I need that the output of this goes to the end of the text.

That is to say:

$textfinal = "Hahahha que legal esse site, consegui responder todas minhas perguntas lá /" *PREVIA DO LINK*

I see it as a good alternative to make a filter with regex or something like (not my area) to be able to capture all kinds of link..

3 answers

1


try as follows:

$string =  SUA_STRING;
preg_match_all('https?://(([^ .]+)\.)+[^ .]{2,4}(/[^ /]+)', $string, $conteudo);

$link = $conteudo[0][0];//link

1

Use the following code:

$links = array();
$text = "Hahahha que legal esse site, consegui responder todas minhas perguntas lá /"
$regexp = "[-a-zA-Z0-9@:%_\+.~\#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~\#?&//=]*)?";
if(preg_match_all("#$regexp#", $text, $matches, PREG_SET_ORDER)) {
    foreach($matches as $match) {
        $links[] = $match[0];
    }
}

print_r($links);

1

My dear, you will need to use a regular expression to get every value you have as a http or https start. It would be something less in this idea:

https?://(([^ .]+)\.)+[^ .]{2,4}(/[^ /]+)*
  • 1

    Yes, and how to perform the function itself?

Browser other questions tagged

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