Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How would I do this?

Status
Not open for further replies.

buzzt

Programmer
Joined
Oct 17, 2002
Messages
171
Location
CA
I want to have my PHP page replace any instance of <link> with <a href=&quot; and </link> with &quot;</a>&quot;. This is so that if a URL is typed in the text file, the PHP page will know to hyperlink it automatically. So,
Code:
<link>www.a.com</link>
would translate to
Code:
<a href=&quot;[URL unfurl="true"]http://www.a.com&quot;[/URL] target=&quot;_blank&quot;>www.a.com</a>

I tried several variations of str_replace and preg_replace, but nothing was quite right. Any ideas?
 
By the way, the text file I am referring to above is an include on the PHP page (in case this wasn't obvious). The text for the PHP page comes from this text file.
 
u cannot directly use the replace method here. i want more infor on this like <link>abc.com</link> will this be on a seperate line or will it be along with some other text?



Known is handfull, Unknown is worldfull
 
Basically, it would look like

Blah blah blah <link>
Code:
www.a.com[code]</link>[/color] blah blah. Any text Any text, any text [COLOR=blue]<link>[code]www.b.com[code]</link>[/color] Any text, any text.

Anything inside the [COLOR=blue]<link> </link>[/color] would become an active link with the URL of that text.
 
$link = preg_replace(&quot;'<link>(.*?)</link>'&quot;,<a href=\&quot;\$1\&quot; target=\&quot;_blank\&quot;>\$1</a>&quot;, $str);

i think that'll do it :]
 
oops

$link = preg_replace(&quot;'<link>(.*?)</link>'&quot;,<a href=\&quot;\$1\&quot; target=\&quot;_blank\&quot;>\$1</a>&quot;, $str);

make it

$link = preg_replace(&quot;'<link>(.*?)</link>'&quot;,&quot;<a href=\&quot;\$1\&quot; target=\&quot;_blank\&quot;>\$1</a>&quot;, $str);


missing &quot; there :x

works ;]
 
jamesp0tter: nice name. it reminds me of a very famous character... ;)

Known is handfull, Unknown is worldfull
 
I recently answered something simular:
thread434-608527

Don't eat yellow snow!
 
Well, it seems to hide the <link> and </link> from view (although it is still there when you view source) but it does not make it a link with the same value.
 
jamesp0tter's idea, that it...
 
Code:
The one jamesp0tter left me writes the link, but at the end of the current domain. So if my web page is [URL unfurl="true"]www.a.com[/URL] and I want to make a link on my web page to [URL unfurl="true"]www.b.net,[/URL] I end up with a final link that looks like www.a.com/www.b.net

I finally found this at PHP.net. It works well. The only real difference is that it automatically makes all the links without having to add the <link> and </link>.

Oh well...

$text = preg_replace(&quot;/\b((http(s?):\/\/)|(www\.))([\w\.-]+)([\/\w+\.]+)\b/i&quot;, &quot;<a href=\&quot;http$3://$4$5$6\&quot; target=\&quot;_blank\&quot;>$4$5$6</a>&quot;, $text);
 
I figured out jamesp0tter's. It works like below...

$content = preg_replace(&quot;'<link>(.*?)</link>'&quot;,&quot;<a href=\&quot;http://\$1\&quot; target=\&quot;_blank\&quot;>\$1</a>&quot;, $content);
 
BTW...Thanks everyone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top