Jump to content

[SOLVED] eregi_replace with Smart Links


saint959

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/147703-solved-eregi_replace-with-smart-links/
Share on other sites

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

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

 

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.

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.