Jump to content

Simple PHP code problem...?


Recommended Posts

All right.

 

More questions.

 

Could you change these regexps from eregi to pregi? I hear preg_ functions are faster.

$article_text = eregi_replace("\[url=([^(javascript)][^\[]+)\]([^\[]+)\[/url\]","<a href=\"\\1\" target=\"_blank\">\\2</a>", $article_text);
$article_text = eregi_replace("\[url\]([^(javascript:)][^\[]+)\[/url\]","<a href=\"\\1\" target=\"_blank\">\\1</a>", $article_text); 

 

 

(working on bbcode tables at the moment)

Link to comment
Share on other sites

 $article_text = preg_replace("#\[url=([^(?:javascript)][^\[]+)\]([^\[]+)\[/url\]#i","<a href=\"\\1\" target=\"_blank\">\\2</a>", $article_text);
$article_text = preg_replace("#\[url\]([^(?:javascript:)][^\[]+)\[/url\]#i","<a href=\"\\1\" target=\"_blank\">\\1</a>", $article_text);

 

Seems you simply needed to include the delimiters at the beginning and end of the regex pattern, and the 'i' modifier to ensure case sensitivity.

 

One thing to note is that [^(?:javascript)] and [^(javascript:)] in both situations (eregi and preg) only prevent the first character after the being j,a,v,a,s,c,r,i,p,t or : (depending on which one). While this does prevent the first word being javascript (as the first letter would have to be J), it also prevents things such as [ url=joako.net ]dfsf[ /url ] or [ url ]joako.net[ /url ] which based on the fact that your not forcing people to have http:// at the beginning seems to suggest SHOULD be valid (but arent). Just thought i'd mention it.

 

Also, the first letter cant be 'j' but it can be a space followed by 'j' which will still work in allowing javascript into the url so you may want to consider revising the regex.

 

 

 $article_text = preg_replace("#\[url=(?!\s*javascript:)([^\[]+)\]([^\[]+)\[/url\]#i","<a href=\"\\1\" target=\"_blank\">\\2</a>", $article_text);
$article_text = preg_replace("#\[url\](?!\s*javascript:)([^\[]+)\[/url\]#i","<a href=\"\\1\" target=\"_blank\">\\1</a>", $article_text);

 

The code above SEEMS to work for what I'm assuming your trying to do.

Link to comment
Share on other sites

Indeed it does work. Thanks.

 

 

However, if someone forgets the http:// part, then the URL becomes relative to the script and it gets rather messy. Do you think I ought to require http:// or just auto-replace? (have one regex for with, one for without)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • 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.