No announcement yet.

preg_replace help!!!

  • Filter
  • Time
  • Show
Clear All
new posts

  • preg_replace help!!!

    Hey everyone.

    I'm directing this mainly at the vB developers since I'm expecting that they've done something very similar with vB3, but of course anyone is welcome to answer.

    I am in the process of implementing a WYSIWYG text editor (like that in vB3) and it's working fine. The problem is that when it is submitted it returns HTML.

    So I've turned to using str_replace and preg_replace functions in order to get rid of HTML I don't want, and convert other things into code (such as <b> to [b]).

    The problem is that of all things in PHP, I find that regular expressions are the hardest! I've been trying for an hour, but can't manage to get things to work.

    The main problem is that the WYSIWYG are can accept text from the clipboard which has been copied from a Word Processing platform such as Word. This is extremely useful since most articles are written in Word before they are added to the site database.

    However because Word never plays easy, it inserts a considerable amount of useless HTML, as well as messing up other tags. Becuase of my lack of experience with Regular Expressions I'm asking for anyone on here who may be able to help, to help me out!

    Below is a sample of the HTML which the form returns after being submitted with some text copied from Word:

    <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=3><FONT face="Times New Roman"><B>Description: </B>In these days of forward thinking and innovative companies, do we really want to have games which emulate those which have gone before it? Manhattan Project returns the Duke Nukem series to it’s roots, but is it any good?<?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o></o></FONT></FONT></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B><FONT size=3><FONT face="Times New Roman">&nbsp;<o></o></FONT></FONT></B></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=3><FONT face="Times New Roman"><B>Article:</B><B><SPAN style="FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt"><o></o></SPAN></B></FONT></FONT></P> <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt"><FONT face="Times New Roman">&nbsp;<o></o></FONT></SPAN></P><SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-GB; mso-fareast-language: EN-US; mso-bidi-language: AR-SA">There are very few gamer’s who won’t have heard of Duke Nukem, and I imagine most have played one of the Duke Nukem games somewhere along the line. For me it started with the first 3D version of the game, Duke Nukem 3D but many others first experienced Duke Nukem before that. In fact while many people wrongly think Duke Nukem properly started with DN3D, they are in fact wrong -- Duke Nukem started it’s life as a platformer, sold as shareware.</SPAN>
    Now I'm sure you can guess that I don't want that kind of thing adding to the database, and since we have a system similar to vB's vB code, I want to convert things like <b> to [b] and <i> to [i]. This isn't a problem, but things like <font> are becuase they require the use of preg_replace functions.

    Can anyone give me some help on how to do this? Here's the simple code I have so far -- all this is, is the str_replace functions to do the easy things! Note in the vB code bits, I've added an extra space so that vBulletin doesn't parse it as vB code (as in I've written [ b] instead of [b])

    PHP Code:
    $bodytext=str_replace("<b>","[ b]",$bodytext);
    $bodytext=str_replace("<B>","[ b]",$bodytext);
    $bodytext=str_replace("</b>","[ /b]",$bodytext);
    $bodytext=str_replace("</B>","[ /b]",$bodytext);

    $bodytext=str_replace("<i>","[ i]",$bodytext);
    $bodytext=str_replace("<I>","[ i]",$bodytext);
    $bodytext=str_replace("</i>","[ /i]",$bodytext);
    $bodytext=str_replace("</I>","[ /i]",$bodytext); 
    Thanks everyone.
widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.