Current location: Hot Scripts Forums » Advertising Forums » Job Offers & Assistance » Need the Reverse Operation of a Function


Need the Reverse Operation of a Function

Reply
  #1 (permalink)  
Old 04-26-05, 08:39 PM
ozwald ozwald is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Need the Reverse Operation of a Function

I need to get the exact opposite of this function that converts UBB-style script tags to HTML tags:

Code:
function parseCode($text){
  $tagArray['img'] = array('open'=>'<img src="','close'=>'">');
  $tagArray['b'] = array('open'=>'<b>','close'=>'</b>');
  $tagArray['i'] = array('open'=>'<i>','close'=>'</i>');
  $tagArray['u'] = array('open'=>'<u>','close'=>'</u>');
  $tagArray['url'] = array('open'=>'<a href="','close'=>'">\\1</a>');
  $tagArray['email'] = array('open'=>'<a href="mailto:','close'=>'">\\1</a>');
  $tagArray['url=(.*)'] = array('open'=>'<a href="','close'=>'">\\2</a>');
  $tagArray['email=(.*)'] = array('open'=>'<a href="mailto:','close'=>'">\\2</a>');
  $tagArray['color=(.*)'] = array('open'=>'<font color="','close'=>'">\\2</font>');
  $tagArray['size=(.*)'] = array('open'=>'<font size="','close'=>'">\\2</font>');
  $tagArray['font=(.*)'] = array('open'=>'<font face="','close'=>'">\\2</font>');
  $sTagArray['br'] = array('tag'=>'<br>');
  $sTagArray['hr'] = array('tag'=>'<hr>');

  foreach($tagArray as $tagName=>$replace){
    $tagEnd=preg_replace('/\W/Ui','',$tagName);
    $text = preg_replace("|\[$tagName\](.*)\[/$tagEnd\]|Ui","$replace[open]\\1$replace[close]",$text);
  }
  foreach($sTagArray as $tagName=>$replace){
    $text= preg_replace("|\[$tagName\]|Ui","$replace[tag]",$text);
  }
  
	return $text;
}
Basically what I need it to do is instead of convert script tags to HTML tags, I need it to convert HTML tags to the script tags ([b], [u], etc.). I'm sure this wouldn't take long for somebody who knows what they're doing, but this is a little bit over my head yet. It'd be nice if someone could do this for free...but I am willing to pay a small amount via PayPal.

Last edited by ozwald; 04-26-05 at 08:59 PM.
Reply With Quote
  #2 (permalink)  
Old 04-27-05, 04:27 AM
darksystem darksystem is offline
Newbie Coder
 
Join Date: Mar 2005
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
learn BB code lol... there's a lot of tutorial on that...just ask google.com and type the keyword "BB code Tutorial"
Reply With Quote
  #3 (permalink)  
Old 04-27-05, 06:46 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
here is a free function
PHP Code:

//if you want to enable images, you have to pass a true in the second argument..

function BBCode($string$allowimgs false) {

        
$from[] = '~\[i\](.*?)\[\/i\]~is';
        
$from[] = '~\[b\](.*?)\[\/b\]~is';
        
$from[] = '~\[u\](.*?)\[\/u\]~is';
        
$from[] = '~\[s\](.*?)\[\/s\]~is';
        
$from[] = '~\[size=([0-9]*?)\](.*?)\[\/size\]~is';
        
$from[] = '~\[color=(.*?)\](.*?)\[\/color\]~is';
        
$from[] = '~\[url=(.*?)\](.*?)\[\/url\]~is';
        
$from[] = '~\[url\](.*?)\[\/url\]~is';
        
$from[] = '~\[center\](.*?)\[\/center\]~is';
        
$from[] = '~\[right\](.*?)\[\/right\]~is';
        
$from[] = '~\[left\](.*?)\[\/left\]~is';
        
$from[] = '~\[sub\](.*?)\[\/sub\]~is';
        
$from[] = '~\[sup\](.*?)\[\/sup\]~is';
        
$from[] = '~\[pre\](.*?)\[\/pre\]~is';
        
$from[] = '~\[marquee\](.*?)\[\/marquee\]~is';
        
$from[] = '~\[marquee=(.*?)\](.*?)\[\/marquee\]~is';
        
$from[] = '~\[quote\](.*?)\[\/quote\]~is';
        
$from[] = '~\[code\](.*?)\[\/code\]~is';

        
$to[] = '<i>\\1</i>';
        
$to[] = '<b>\\1</b>';
        
$to[] = '<u>\\1</u>';
        
$to[] = '<del>\\1</del>';
        
$to[] = '<span style="font-size: \\1pt;">\\2</span>';
        
$to[] = '<span style="color: \\1;">\\2</span>';
        
$to[] = '<a href="\\1">\\2</a>';
        
$to[] = '<a href="\\1">\\1</a>';
        
$to[] = '<div align="center">\\1</div>';
        
$to[] = '<div align="right">\\1</div>';
        
$to[] = '<div align="left">\\1</div>';
        
$to[] = '<sub>\\1</sub>';
        
$to[] = '<sup>\\1</sup>';
        
$to[] = '<pre>\\1</pre>';
        
$to[] = '<marquee>\\1</marquee>';
        
$to[] = '<marquee direction="\\1">\\2</marquee>';
        
$to[] = '<table border="0" width="80%" align="center"><tr><td><span class="quote_head">Quote:</span></td></tr><tr><td><table align="center" width="100%" border="1" class="quote_cell"><tr><td>\\1</td></tr></table></td></tr></table>';
        
$to[] = '<table border="0" width="80%" align="center"><tr><td><span class="code_head">Code:</span></td></tr><tr><td><table align="center" width="100%" border="1" class="code_cell"><tr><td class="code">\\1</td></tr></table></td></tr></table>';


    
//we don't want to allow imgs all the time!
        
if ($allowimgs == true) {
           
$from[] = '~\[img\](.*?)\[\/img\]~is';
           
$to[]   = '<img src="\\1" border="0" alt="" />';
        }

        
$string preg_replace($from$to$string);
         return 
$string;

example:
PHP Code:

$text '[b]BBcode[/b] function with [i]italic[/i] and [img]http://www.domain.com/image.jpg[/img]';


$text_without_img BBCode($text); //will NOT parse [img] tags! [/img]

$text_with_img BBCode($texttrue); //this will parse the [img] 
HTH

P.S. I suggest you do a little search next time before offering money
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #4 (permalink)  
Old 04-27-05, 07:51 AM
ozwald ozwald is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
I understand there are plenty of free scripts to convert BB code to HTML, but I need to convert HTML to BB code. I haven't been able to find a script that does that.
Reply With Quote
  #5 (permalink)  
Old 04-27-05, 04:44 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
opps...
my bad, I didn't read carefully
what are the tags you would like to convert from HTML to BBCode?

here is a function that will convert the <b><u><i><del> to their BBcodes equivelent:
PHP Code:

function HTML2BBCode($string)

{
        
$from[] = '~<i>(.*?)<\/i>~is';
        
$from[] = '~<b>(.*?)<\/b>~is';
        
$from[] = '~<u>(.*?)<\/u>~is';
        
$from[] = '~<del>(.*?)<\/del>~is'

        
$to[] = '[i]\\1[/i]';
        
$to[] = '[b]\\1[/b]';
        
$to[] = '[u]\\1[/u]';
        
$to[] = '[s]\\1[/s]';

       
$string preg_replace($from$to$string);

       return 
$string;

this is the basic of how you do it.. not tested tho
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 04-27-05 at 05:31 PM.
Reply With Quote
  #6 (permalink)  
Old 04-27-05, 04:59 PM
ozwald ozwald is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
No problem.

But I see, it's basically just changing the <> and [] around...it's just the ones for like images and font color and stuff that confused me.

I did dive a little deeper into a google search today, tho, and I found one that uses str_replace() instead of preg_replace()...the code for that one is much more simple, and I think I should be able to reverse it no problem.

Thanks for the help, tho.
Reply With Quote
  #7 (permalink)  
Old 04-27-05, 05:32 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
sorry, I forgot to rename the $to[]s to $from[]s..
code edited above..

the problem with str_replace() is case-sensitivity..
preg_replace() with flag i makes it case-insensitive and that's an advanatge unless you are using PHP5 where you can use str_ireplace().
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Scrollbar!!!!!! LiLSweetie HTML/XHTML/XML 8 07-22-04 05:01 PM
PHP Error Fairnie PHP 8 06-26-04 07:15 AM
Disable form fields to be submitted RickyRod JavaScript 2 05-24-04 10:15 AM
accessing existing ISP email with a PHP webmail script. nlancaster PHP 1 01-07-04 03:28 AM
Help trim code down TheLaughingBandit JavaScript 0 09-02-03 09:50 AM


All times are GMT -5. The time now is 12:13 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.