JasonLewis Posted July 29, 2008 Share Posted July 29, 2008 Howdy. This is driving my insane. I just can't think anymore. Okay I am trying out my regular expression knowledge by creating my own BBCode Parser. Now, I have it parsing stuff in url tags, similar to what is used on this website. The thing is, I want to convert text written as www.website.com or http://website.com or http://www.website.com or website.com into active links. The problem isn't doing it, it's re-linking the ones that are already linked. Basically, I only want to do it to text that isn't already linked. Is there any pattern I can use that will allow me to do this? Cheers all! Quote Link to comment Share on other sites More sharing options...
effigy Posted July 29, 2008 Share Posted July 29, 2008 How about this? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 30, 2008 Author Share Posted July 30, 2008 Cheers, I swear I searched and it returned nothing. Thanks for that effigy. I'll try it out when I get the chance. I'm sure it'll be what I need, from just reading it. Thanks. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 30, 2008 Author Share Posted July 30, 2008 Okay this is the expression I'm using. It's working, except if the link is like this: [url=www.website.com]some text[/url] Or anything as long as the middle text has a space, this expression: preg_replace(% ### Not preceded by an url start. (?<!url[=]]) ( ### Protocol or start. (?: (??:https?|ftp)://) | www. ) ### Body. (?> ### Gobble all non-space with the exception of [/url] (??!\[/url\])\S)+ ### Avoid ending punctuation. (?<!\p{P}) ) ### Not followed by an url end. (?!\[/url\]) ) %x', '<a href="$1">$1</a>', $data); Will match it. How can I make it so that it won't match it when the middle text has spaces? Cheers. EDIT: I just removed the \S. Just let me know if that's an appropriate solution of if there is something better. Thanks. Quote Link to comment Share on other sites More sharing options...
effigy Posted July 30, 2008 Share Posted July 30, 2008 Oddly enough, the code that is displayed is not the same as the code entered. Try this: echo preg_replace('% ### Not preceded by an url start. (?<!url[=\]]) ( ### Protocol or start. (?: (??:https?|ftp)://) | www\. ) ### Body. (?> ### Gobble all non-space with the exception of [/url] (??!\[/url\])\S)+ ### Avoid ending punctuation. (?<!\p{P}) ) ### Not followed by an url end. (?!\[/url\]) ) %x', '<a href="$1">$1</a>', $data); Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 31, 2008 Author Share Posted July 31, 2008 Cheers, that did it. Removing the \S wasn't a very good idea. Thanks for everything effigy. Quote Link to comment 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.