saint959 Posted March 3, 2009 Share Posted March 3, 2009 Hi All, I really hope someone can help me, i have been struggling with this for a while now and i just cant seem to come right. I have a text field where a "client" can enter links the standard way i.e. www.url1.co.za which i then convert to a link when the text gets displayed. That is working perfectly. It makes the link: <a href="http://www.url1.co.za">www.url1.co.za</a> My problem comes in here: I also (trying to) allow them to enter "smarter" links i.e. [Link]This is the link text[/Link] www.url2.co.za This makes: <a href="www.url2.co.za">This is the link text</a> Now i have gotten the above to work when the [Link][/Link] tags have only been entered once with the text field, but the second they try and enter two the whole thing gets mixed up.... Here is the code i have so far: function makelinks($text) { //for the "smart" links $text_new = eregi_replace('\[Link\](.*)\[/Link\]([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="http://\\3" target="_blank">\\1</a>', $text_new); $text_new = eregi_replace('\[Link\](.*)\[/Link\]([[:space:]()[{}])(http://[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\3" target="_blank">\\1</a>', $text_new); //for the standard links (works perfectly) $text_new = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2">\\2</a>', $text_new); $text_new = eregi_replace('([[:space:]()[{}])(http://[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="\\2">\\2</a>', $text_new); } If anyone could help that would really HELP ALOT Quote Link to comment https://forums.phpfreaks.com/topic/147703-solved-eregi_replace-with-smart-links/ Share on other sites More sharing options...
HuggieBear Posted March 3, 2009 Share Posted March 3, 2009 Why don't you format it like the forum here... [link=www.url1.co.za][/link] [link=www.url1.co.za]This is the link text[/link] Quote Link to comment https://forums.phpfreaks.com/topic/147703-solved-eregi_replace-with-smart-links/#findComment-775340 Share on other sites More sharing options...
saint959 Posted March 3, 2009 Author Share Posted March 3, 2009 Hey I had a look around and I also tried this: <?php function makelinks($text) { //for the "smart" links $text_new = eregi_replace('\[Link\](.*?)\[/Link\]([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="http://\\3" target="_blank">\\1</a>', $text_new); $text_new = eregi_replace('\[Link\](.*?)\[/Link\]([[:space:]()[{}])(http://[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\3" target="_blank">\\1</a>', $text_new); //for the standard links (works perfectly) $text_new = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2">\\2</a>', $text_new); $text_new = eregi_replace('([[:space:]()[{}])(http://[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="\\2">\\2</a>', $text_new); } ?> But i then get this: Warning: eregi_replace() [function.eregi-replace]: REG_BADRPT Quote Link to comment https://forums.phpfreaks.com/topic/147703-solved-eregi_replace-with-smart-links/#findComment-775344 Share on other sites More sharing options...
saint959 Posted March 3, 2009 Author Share Posted March 3, 2009 Ill definitely look into that thanks! Sometimes you just think complex when simple may be the best solution. Quote Link to comment https://forums.phpfreaks.com/topic/147703-solved-eregi_replace-with-smart-links/#findComment-775364 Share on other sites More sharing options...
saint959 Posted March 3, 2009 Author Share Posted March 3, 2009 Thanks HuggieBear, I am sitting with the same problem though... If there are more than one within a textfield it gets confused. I read here that soemthing i should do is: <?php $text_new = eregi_replace('\[link=(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)\](.*?)\[/link\]', '<a href="http://\\1" target="_blank">\\2</a>', $text_new); ?> i.e. the ? after the (.* .... but this is giving me an error? have you got any idea for me? the error is: Warning: eregi_replace() [function.eregi-replace]: REG_BADRPT Quote Link to comment https://forums.phpfreaks.com/topic/147703-solved-eregi_replace-with-smart-links/#findComment-775366 Share on other sites More sharing options...
saint959 Posted March 3, 2009 Author Share Posted March 3, 2009 Hi, no worries, i have managed to come right. I changed the stuff around and got it sorted. Quote Link to comment https://forums.phpfreaks.com/topic/147703-solved-eregi_replace-with-smart-links/#findComment-775409 Share on other sites More sharing options...
HuggieBear Posted March 3, 2009 Share Posted March 3, 2009 Well done, I didn't make it back in time to reply. Can you post your fixed version so other's can see what you did. Quote Link to comment https://forums.phpfreaks.com/topic/147703-solved-eregi_replace-with-smart-links/#findComment-775441 Share on other sites More sharing options...
nrg_alpha Posted March 3, 2009 Share Posted March 3, 2009 I would not advise using ereg, as this functionality (which is part of POSIX - Portable Operating System Interface) will not be included within the core of PHP as of version 6 by default. I would instead learn PCRE (Perl Compatible Regular Expressions) [preg] instead. Future proof your code now while you can. You can read up about preg in a number of places such as the PHP manual: PCRE. You can also view phpfreak's resource page as well. Quote Link to comment https://forums.phpfreaks.com/topic/147703-solved-eregi_replace-with-smart-links/#findComment-775670 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.