johnsmith153 Posted September 9, 2008 Share Posted September 9, 2008 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 More sharing options...
DarkWater Posted September 9, 2008 Share Posted September 9, 2008 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; Link to comment https://forums.phpfreaks.com/topic/123356-solved-darkwater-needed/#findComment-637125 Share on other sites More sharing options...
DarkWater Posted September 9, 2008 Share Posted September 9, 2008 I just wrote that now, and there are probably some legitimate characters that I missed in the ending, but that should suffice. EDIT: What's a DarkWater? o_O Link to comment https://forums.phpfreaks.com/topic/123356-solved-darkwater-needed/#findComment-637129 Share on other sites More sharing options...
johnsmith153 Posted September 9, 2008 Author Share Posted September 9, 2008 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. Link to comment https://forums.phpfreaks.com/topic/123356-solved-darkwater-needed/#findComment-637141 Share on other sites More sharing options...
DarkWater Posted September 9, 2008 Share Posted September 9, 2008 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; Link to comment https://forums.phpfreaks.com/topic/123356-solved-darkwater-needed/#findComment-637144 Share on other sites More sharing options...
johnsmith153 Posted September 9, 2008 Author Share Posted September 9, 2008 Perfect. Link to comment https://forums.phpfreaks.com/topic/123356-solved-darkwater-needed/#findComment-637159 Share on other sites More sharing options...
DarkWater Posted September 9, 2008 Share Posted September 9, 2008 No problem. Please mark this topic as solved. Link to comment https://forums.phpfreaks.com/topic/123356-solved-darkwater-needed/#findComment-637161 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.