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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.