Jump to content

[SOLVED] DarkWater Needed


johnsmith153

Recommended Posts

I need to be able to search through in a string and display it as a hyperlink if it requires it. (I dont want user to have to confirm, or put tags around it etc. - just type normal message.)

 

i.e

<?php
$value = "Why dont you check this http://www.greatsite.com out";
?>

I then need to do: echo 'Why dont you check this <a href="http://www.greatsite.com" target="_blank">http://www.greatsite.com</a>'

 

How would I do this? I imagine it would be various search functions to pull out certain data etc.

 

Also, would need to pick up when string contains just www. etc.

 

It is for a message board, so cant expect people to type http://

 

Interesting if PHP Freaks does this:?

http://www.phpfreaks.com

www.phpfreaks.com

Link to comment
https://forums.phpfreaks.com/topic/123356-solved-darkwater-needed/
Share on other sites

You could try:

 

<?php
//assume $text is your post
$text = "This is just a post...with a link to http://www.google.com/somepage!";
$text = preg_replace('!(http://)?(www\.[a-z._-]+?\.[a-z]+(/[a-z0-9._+,-/]+)?)!i', '<a href="http://$2">$0</a>', $text);
echo $text;

Excellent.

 

However, common problem with .co.uk.

 

If you try a English web address - i.e www.englandisgreat.co.uk - it doesn't actually work (it only picks up the www.englandisgreat.co)

 

Would appreciate what I am sure is a minor change required to also pick up .uk if it is at the end. Of course cant pick up anything after a second . because this may be a full stop, and someone may forget the space after the full stop - i.e $value="this is the web address http://www.sitename.com.Let me know what you think.";

 

test:

www.howdoesphpfreaksdealwithcouk.co.uk

this is the web address http://www.sitename.com.Let me know what you think

 

Thanks again anyway.

Ah, good point.  Here you go:

 

<?php
//assume $text is your post
$text = "This is just a post...with a link to http://www.google.com/somepage!";
$text = preg_replace('!(http://)?(www\.[a-z._-]+?\.[a-z]{1,4}\.?[a-z]{1,4}(/[a-z0-9._+,-/]+)?)!i', '<a href="http://$2">$0</a>', $text);
echo $text;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.