Answer the question
In order to leave comments, you need to log in
How can I extract a link from a PHP string?
Hello. How can I find the URL in the text and put it in the "[a]" tag using php?
For example, there is a message "Hi! My page is localhost/mypage123 Go here!"
The final HTML page should look like this: "Hi! My page is [a href="/away/ localhost/mypage123 "] http://localhost/mypage123[/a] Go here everyone!"
This is done in VK and on other services. Thank you!
Answer the question
In order to leave comments, you need to log in
$text = "Моя страница http://localhost/user/1 Переходите все сюда! http://localhost/user/1";
function replace_href($text)
{
$words = explode(' ', $text);
for ($i=0; $i < count($words); $i++)
{
if(strpos($words[$i], 'http://') !== false || strpos($words[$i], 'https://'))
{
$words[$i] = '<a href="'.$words[$i].'">'.$words[$i].'</a>';
}
@$new_text = $new_text.' '.$words[$i];
}
return @$new_text;
}
echo replace_href($text);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question