Old 12-04-2005   #1 (permalink)
Sheep Worrier
 
Man1c M0g's Avatar
 
Join Date: Sep 2003
Location: Portsmouth, UK.
Posts: 4,105
Blog Entries: 14

Send a message via ICQ to Man1c M0g Send a message via MSN to Man1c M0g Send a message via Skype™ to Man1c M0g
Regular Expressions Nightmare

OK, at the moment I am using this regular expression-based function to process a BBcode-based message and translate it into HTML-encoding:

PHP Code:
function bbcodetranslate($string){ 
$string nl2br(htmlspecialchars($string)); 
$patterns = array(
    
'`\[center\](.+?)\[/center\]`is'
    
'`\[b\](.+?)\[/b\]`is'
    
'`\[i\](.+?)\[/i\]`is'
    
'`\[u\](.+?)\[/u\]`is'
    
'`\[strike\](.+?)\[/strike\]`is'
    
'`\[color=(.+?)\](.+?)\[/color\]`is'
    
'`\[email\](.+?)\[/email\]`is'
    
'`\[img\](.+?)\[/img\]`is'
    
'`\[url=([a-z0-9]+://)([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\](.*?)\[/url\]`si'
    
'`\[url\]([a-z0-9]+?://){1}([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)\[/url\]`si'
    
'`\[url\]((www|ftp)\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\[/url\]`si'
    
'`\[flash=([0-9]+),([0-9]+)\](.+?)\[/flash\]`is'
    
'`\[quote\](.+?)\[/quote\]`is',
    
'`\[quote=(.+?)\](.+?)\[/quote\]`is',
    
'`\[indent](.+?)\[/indent\]`is'
    
'`\[size=([1-6]+)\](.+?)\[/size\]`is',
    
'`\[code\](.+?)\[/code\]`is',
    
'`\[html\](.+?)\[/html\]`is',
    
'`\[php\](.+?)\[/php\]`is',
    
'`\[news\](.+?)\[/news\]`is'
                                
); 
$replaces =  array( 
    
'<center>\\1</center>'
    
'<strong>\\1</strong>'
    
'<em>\\1</em>'
    
'<span style="border-bottom: 1px dotted">\\1</span>'
    
'<strike>\\1</strike>'
    
'\2'
    
'<a href="mailto:\1">\1</a>'
    
'<img src="\1" alt="" style="border:0px;" />'
    
'<a href="\1\2">\6</a>'
    
'<a href="\1\2">\1\2</a>'
    
'<a href="http://\1">\1</a>'
    
'<object width="\1" height="\2"><param name="movie" value="\3" /><embed src="\3" width="\1" height="\2"></embed></object>'
    
'<table style="margin:0px 5px;padding:5px;background-color:#F8F9F1;border:1px solid #CCCCCC;width:95%;" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td><strong>Quote:</strong><em>\1</em></td></tr></table>',
    
'<table style="margin:0px 5px;padding:5px;background-color:#F8F9F1;border:1px solid #CCCCCC;width:95%;" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td><strong>Quote from \1:</strong><br><em>\2</em></td></tr></table>'
    
'<pre>\\1</pre>'
    
'<h\1>\2</h\1>',
    
'<table class="code" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td>\1</td></tr></table>',
    
'<table class="code" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td>\1</td></tr></table>',
    
'<table class="code" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td>\1</td></tr></table>',
    
'<table width="90%" align="center" border="0"><tr><td><p align="justify"><i>\1</i></td></tr></table>'
                                    
); 
$string preg_replace($patterns$replaces $string); 
return 
$string

Essentially, this means that when I use this string:
Code:
[url =http://blah.com]Blah[ /url] (Ignore the spaces)
I am given a url link. The thing is that this regular expression does not make use of quotation marks around the URL string itself.. something that is a newish function of Vbulletin v3.5.x. How would I amend my initial function to include translation for BOTH bbcodes with and without quotations? I've fiddled with this for a long time, and I'm pretty sure I must be missing something obvious...
__________________
Man1c M0g is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 12-05-2005   #2 (permalink)
code anyone?
 
scrowler's Avatar
 
Join Date: Feb 2004
Location: New Zealand
Posts: 590

Send a message via MSN to scrowler Send a message via Skype™ to scrowler
did you try adding new search/replace array entries? try adding this entry in to the first array:

Code:
'`\[url=\"([a-z0-9]+://)([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\"\](.*?)\[/url\]`si'
and this entry into the second array:

Code:
'<a href="\1\2">\6</a>'
make sure you put them both into the array so that they will both have the same key... this should work
__________________
BioRUST Tutorials - the birthplace
scrowler is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 12-05-2005   #3 (permalink)
Sheep Worrier
 
Man1c M0g's Avatar
 
Join Date: Sep 2003
Location: Portsmouth, UK.
Posts: 4,105
Blog Entries: 14

Send a message via ICQ to Man1c M0g Send a message via MSN to Man1c M0g Send a message via Skype™ to Man1c M0g
Ahhhh - that is indeed the first thing I tried... but it just doesn't work! I can't figure out a reason why... it SHOULD work!
__________________
Man1c M0g is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Old 05-02-2006   #4 (permalink)
robin
 
robin746's Avatar
 
Join Date: May 2006
Location: Eire
Posts: 48

One of the reasons I like Python is that, while I *can* use regular expressions, there are generally better ways around the problem. I am sorry this does not help if you are stuck with PHP. Instead I leave you with this dollop of much-quoted humour.

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
-- Jamie Zawinski
__________________
"out of environment space"
robin746 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Wallpaper] Windows Vista Wallpaper Pack - Regular and WideScreen Wallpapers akkasone Showrooms & Works In Progress 4 12-10-2005 12:26 AM
Regular Expression bMoz HTML / PHP / ASP / JS 2 07-11-2004 06:43 AM


All times are GMT +1. The time now is 05:55 PM.
Content Relevant URLs by vBSEO 3.2.0

Design & Content © BioRUST 2008 :: PRIVACY STATEMENT :: LEGAL INFORMATION :: ADVERTISING MEDIA KIT