PUCUK
PUCUK

Reputation: 37

warp string url into html link tag

so i trying to replace url(like www.google.com) into html link tag like this

return preg_replace('"\b(www.\S+)"', '<a href="$1" style="color:white;">$1</a>', $url);

this is modified preg_replace from somewhere else and i still don't fully understand how it works....

so if i using this i will get

<a href"www.google.com" style="color:white;">www.google.com</a>

and this mean it will look for www.google.com in my web directory and not opening www.google.com website

what i trying to achieve is

<a href"http://www.google.com" style="color:white;">google</a>

so how to achieve something like that? and it will be better if when i type

http://www.google.com

then it will also replace

http://www.

too.... if my configuration for current is just replace www. but if i type with http:// then it will only warp from www.google.com and leaving http://

edit: so i want a flexibel one, so if i type www.google.com or http://www.google.com it will catch it and turn it into link tag.... and also if i type like https://stackoverflow.com/posts/26154124/ it will only show like

<a href"https://stackoverflow.com/posts/26154124/" style="color:white;">stackoverflow</a>

2nd edit : i sorry, i forgot that say that i also want to keep other text within the link.. so it will be like i'm typing "here is a big news in www.xxxxx.com" and when i display it/ it will be "here is a big news in xxxxx" where xxxxx is gonna be clickable links... and i trying to do this because i'm gonna use it for my website announcement part...

can we do something like that?

update:

i have thinking about some idea, how about first we do preg_replace to catch http:// and remove it, after that we catch www. to warp it into html link tag and also catch www. and everything afer .com/.co/or .whatever and remove it all until the next space

but i don't know how to use preg_replace since i have no idea how it works

Upvotes: 0

Views: 325

Answers (3)

Khaja Md Sher E Alam
Khaja Md Sher E Alam

Reputation: 906

you were always right but just a simple typing mistake was done

<a href"http://stackoverflow.com/posts/26154124/" style="color:white;">stackoverflow</a>

You missed the '=' equal sign after href. please try this instead

<a href="http://stackoverflow.com/posts/26154124/" style="color:white;">stackoverflow</a>

hope it helps...

Upvotes: 0

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

This is about the closest I could come up with, in order to do what you want to achieve.

There are others ways I'm sure, but you can give the following a try.

You could have the following inside a text file: (you can have 1 or more lines)

bookmark.txt (in the exact format shown) - You don't need to put the http:// it's automatic.

Sidenote: You can use google.com or www.google.com - Only the word Google will appear as the link, but the full href will appear in the HTML source and be a valid URL.

Google google.com
Yahoo yahoo.com
stackoverflow stackoverflow.com/posts/26154124

Then use the following PHP:

<?php 
$file = "bookmark.txt";
$lines = file($file);
foreach($lines as $line)
{
    $piece=explode(" ",$line);
    $link=trim($piece[1]);
    $text=trim($piece[0]);
    echo '<a href="http://'.$link.'">'.$text.'</a><br>';
}

Notes: The " " in $piece=explode(" ",$line); represents a space being the seperator.

You can change it to $piece=explode("|",$line); then changing the text file to:

Google|google.com
Yahoo|yahoo.com
stackoverflow|stackoverflow.com/posts/26154124

should you decide to use something like Google Search

I.e.:

Google Search|google.com
Yahoo|yahoo.com
stackoverflow link|stackoverflow.com/posts/26154124

You can also try:

$content = "http://stackoverflow.com/posts/26154124";
$content = preg_replace('$(https?://[a-z0-9_./?=&#-]+)(?![^<>]*>)$i', ' <a href="$1" target="_blank">StackOverflow</a> ', $content." ");

echo $content;

But at this point and reading your question over and over including your edit, I am not entirely sure that is what you want, and stands at being very broad and beyond the scope of my knowledge with PHP.

Upvotes: 2

Snowman
Snowman

Reputation: 47

You mean this?

 $text = ' This is a text with the link of google.com. And another phrase with google.com url.';
 echo str_replace('google.com','<a href="http://google.com" target=_blank>google</a>',$text);

Upvotes: 0

Related Questions