daneth1712 Posted December 3, 2009 Share Posted December 3, 2009 Hi, I am having a little problem.... I have this function currently, itsort of work, but doesnt do the entire job I need. function format_html($content) { $content = "<p>" . str_replace("\r\n", "<br/>", $content) . ""; $content = "" . str_replace("<br/><br/>", "</p><p>", $content) . ""; $in=array( '`((?:https?|ftp)://\S+[[:alnum:]]/?)`si', '`((?<!//)(www\.\S+[[:alnum:]]/?))`si' ); $out=array( '<a href="$1"/>$1</a> ', '<a href="http://$1"/>$1</a>' ); return preg_replace($in,$out,$content); } Basically, I am calling for some text from a database, adding in <p> tags and checking for url, where some thing like www.something.com has been added and creating a clickable link from it. This works, however only once, if another link is included anywhere with the text, bith links get jumbled up... I also want to be able to include into the function the ability to check for email addresses, and include the mailto tag into that. Can anyone help me get this working? I would really appreciate any help. Thanks daneth1712 Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/ Share on other sites More sharing options...
cags Posted December 3, 2009 Share Posted December 3, 2009 Copied from the user submitted example on PHP.net? Can you provide an example input for us? Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/#findComment-970423 Share on other sites More sharing options...
daneth1712 Posted December 3, 2009 Author Share Posted December 3, 2009 tbh this is 2 functions I have found and tried to piece together. The function works... to a degree. If I have text for example.... "Some random text to use as example Please click here to for more information www.somelink.com" The function includes the <p> tags, and includes the link correctly hyperlinked. If... I added more than one link into the text, for example... www.somelink.com www.anotherlink.com both the links appear link below; http://www.somelink.com%3c/p%3E%3Cp%3Ewww.somewhere.com I also need to be able to add mailto tags for email addresses. I hope this example proved useful.... Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/#findComment-970436 Share on other sites More sharing options...
cags Posted December 3, 2009 Share Posted December 3, 2009 I would suggest that in future when posting example inputs you post them in code tags, because otherwise the forum does exactly what your trying to do and highlights them which can be a pain for other people. Couple of things I'd say about your code. Firstly I'd replace... str_replace("\r\n", "<br/>", $content) // with nl2br($content); The nl2br function was built for the job and will also pick up just \n etc if they make their way into your source. You will also need to add a "</p>" tag at the end of $content to make it 'valid' HTML. With regards to the reason you posted, saying that it breaks your input, your code works fine on the example you gave me. function format_html($content) { $content = "<p>" . str_replace("\r\n", "<br/>", $content) . ""; $content = "" . str_replace("<br/><br/>", "</p><p>", $content) . ""; $in=array('`((?:https?|ftp)://\S+[[:alnum:]]/?)`si','`((?<!//)(www\.\S+[[:alnum:]]/?))`si'); $out=array('<a href="$1"/>$1</a> ','<a href="http://$1"/>$1</a>'); return preg_replace($in,$out,$content); } $input = "If... I added more than one link into the text, for example... www.somelink.com www.anotherlink.com"; echo format_html($input); Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/#findComment-970440 Share on other sites More sharing options...
daneth1712 Posted December 3, 2009 Author Share Posted December 3, 2009 2 links within the same variable still doesnt work for me. This is how it appears: Some dummy text www.somethingnice.com (which displays the link http://www.somethingnice.com%3c/p%3E%3Cp%3EWith) With more dummy text (the word 'With' is also hyperlinked) www.someotherlink.com (which displays the link http://www.someotherlink.com%3cbr) i have no idea what is causing this.... also, when i replace that line with your nl2br($content); i get everything included from the first link into a hyperlink, so like an entire paragraph of text hyperlinked Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/#findComment-970460 Share on other sites More sharing options...
cags Posted December 3, 2009 Share Posted December 3, 2009 So are you saying that if you copy the code I posted in my last post (the bottom code section) into a new script and run it, that it gives you that output? Also I'll say it again, post example in code tags so the forum DOESN'T parse them. Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/#findComment-970462 Share on other sites More sharing options...
cags Posted December 3, 2009 Share Posted December 3, 2009 Nevermind, I looked at the status bar to see what the actual link says. The problem is actually fairly obvious. Your Regular Expression pattern, matches the url untill it finds a whitespace character, you have already replaced carriage returns with '<br/>' tags. This means there are no whitespace character untill the next space is encountered. Move the preg_replace section to before you replace the newline characters and the pattern should work alot better. Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/#findComment-970466 Share on other sites More sharing options...
daneth1712 Posted December 3, 2009 Author Share Posted December 3, 2009 Hi, Thanks for your help, I managed to get it working, I have pasted the code below incase anyone is interested in viewing it. I still need to include the email (mailto) check, any help on offer is more than welcome, but here is the code I have got working. function linkreplace($content_links){ $in=array( '`((?:https?|ftp)://\S+[[:alnum:]]/?)`si', '`((?<!//)(www\.\S+[[:alnum:]]/?))`si'); $out=array( '<a href="$1"/>$1</a> ', '<a href="http://$1"/>$1</a>'); $content_links = preg_replace($in,$out,$content_links); return($content_links); } function format_html($content){ $content_links=$content; $str=linkreplace($content_links); $content=$str; $content = "<p>" . str_replace("\r\n", "<br/>", $content) . ""; $content = "" . str_replace("<br/><br/>", "</p><p>", $content) . ""; return ($content); } Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/#findComment-970632 Share on other sites More sharing options...
cags Posted December 3, 2009 Share Posted December 3, 2009 It really depends what percentage of e-mail addresses you wish to accept as to how complex your pattern is. Searching for e-mail regular expression will give you hundreds of different patterns for validating e-mails, you should be able to use one of those but removing the ^ and $ from the end respectively. Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/#findComment-970635 Share on other sites More sharing options...
daneth1712 Posted December 3, 2009 Author Share Posted December 3, 2009 Sorry, I think I probably should have been alot clearer with what it is ecactly I was trying to do. I have a very basic cms type system, where by I have a form that allows a user to enter a title, image and text which gets stored on the database. I then have a page that pulls all this information and displays a page with the info that had been added. When I pull the text down from the database, this text can be between 4-12 paragraphs of text, and also contain hyperlinks (that would have just been added as www.somewhere.com for example) or email links within the text. The function is supposed to check through the text that has been pulled, add the breaks or p tags where needed and check for any hyperlinks that need to be included. This works fine for standard links (www.) and adds the <p> tags correctly, however I am not sure what to use to check the text for email addresses, as www. is standard to use its fairly simple, an email address its not quite the same method. Hopefully that has made a little more sense. Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/#findComment-970676 Share on other sites More sharing options...
cags Posted December 3, 2009 Share Posted December 3, 2009 Yes, I know thats what you are trying todo, the advice still stands. Finding a Regex Pattern for e-mail will take 10 seconds with a search engine, you might aswell do it as me. Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/#findComment-970705 Share on other sites More sharing options...
daneth1712 Posted December 4, 2009 Author Share Posted December 4, 2009 Hi, I tried that already, but I cant get it to work. Here is my amended code if someone could kindly advise where I am going wrong? /* CHECK AND INSERT HYPERLINKS */ function linkreplace($content_links){ $in=array( '`((?:https?|ftp)://\S+[[:alnum:]]/?)`si', '`((?<!//)(www\.\S+[[:alnum:]]/?))`si', '`((?:mailto:)?([A-Z0-9._%-]+@[A-Z0-9.-]+/.[A-Z]{2,4})/)`si' ); $out=array( '<a href="$1"/>$1</a> ', '<a href="http://$1"/>$1</a>', '<a href="mailto:$1"/>$1</a>'); $content_links = preg_replace($in,$out,$content_links); return($content_links); } /* FORMATS LINE BREAKS WITH PROPER HTML TAGS */ function format_html($content){ $content_links=$content; $str=linkreplace($content_links); $content=$str; $content = "<p>" . str_replace("\r\n", "<br/>", $content) . ""; $content = "" . str_replace("<br/><br/>", "</p><p>", $content) . ""; return ($content); } the lines I have added are '`((?:mailto:)?([A-Z0-9._%-]+@[A-Z0-9.-]+/.[A-Z]{2,4})/)`si' and '<a href="mailto:$1"/>$1</a>'); Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/#findComment-971111 Share on other sites More sharing options...
cags Posted December 4, 2009 Share Posted December 4, 2009 And the example input that your testing it with? Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/#findComment-971117 Share on other sites More sharing options...
daneth1712 Posted December 4, 2009 Author Share Posted December 4, 2009 any text... All I have done is add an email address within the 10 paragraphs of text, the links get picked up, the breaks get inserted, but it doesnt convert the email addresses into clickable links. Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/#findComment-971120 Share on other sites More sharing options...
cags Posted December 4, 2009 Share Posted December 4, 2009 I don't know why the forward slash is before the fullstop I think it should be an escape character (backslash) I also don't know why the other slash is there also in the replace pattern $1 matches the whole match since. I also don't know why there's brackets around the whole pattern. Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/#findComment-971131 Share on other sites More sharing options...
daneth1712 Posted December 4, 2009 Author Share Posted December 4, 2009 Hi, Sorry I dont understand what you are advising... can you please explain? Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/#findComment-971141 Share on other sites More sharing options...
cags Posted December 4, 2009 Share Posted December 4, 2009 See how it makes things more difficult when people are vague? Change... '`((?:mailto:)?([A-Z0-9._%-]+@[A-Z0-9.-]+/.[A-Z]{2,4})/)`si' ...to... '`(?:mailto:)?([A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4})`si' Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/#findComment-971145 Share on other sites More sharing options...
daneth1712 Posted December 4, 2009 Author Share Posted December 4, 2009 nevermind, I fixed it myself. Thanks for your help though. This is the code I used to get it working.... function linkreplace($content_links){ $in=array( '`((?:https?|ftp)://\S+[[:alnum:]]/?)`si', '`((?<!//)(www\.\S+[[:alnum:]]/?))`si', '`((\S+@)\S+[[:alnum:]]/?)`si' ); $out=array( '<a href="$1"/>$1</a> ', '<a href="http://$1"/>$1</a>', '<a href="mailto:$1"/>$1</a>' ); $content_links = preg_replace($in,$out,$content_links); return($content_links); } Link to comment https://forums.phpfreaks.com/topic/183845-function-to-make-links-clickable/#findComment-971151 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.