Jump to content

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;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.