Welcome to Dream.In.Code
Become a PHP Expert!

Join 150,170 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,286 people online right now. Registration is fast and FREE... Join Now!




Regex Multiple Lines

 
Closed TopicStart new topic

Regex Multiple Lines

Addiction2Code
31 Aug, 2008 - 11:34 AM
Post #1

New D.I.C Head
*

Joined: 19 Dec, 2006
Posts: 39


My Contributions
Hello, I have to parse blocks of code on my website, the user uses BBCode, just like this site with the code tags, the only problem I have is, my regex doesn't do multiple lines, I've searched all over the web and cant quite figure it out, so I figured I would ask here.

This is the block of code

CODE

// convert [ code ]code[ /code ]

$pattern[] = '/\[ code \]([.\n]*)\[\/ code \]/i';

$replace[] = codize('$1');


This works if the code is only one like without \n, but almost all code is more then one line. When I run it just returns the code tags with the code inside them... nothing actually happens.
Thanks in advance for the help!

NOTE: I added spaces in the code block, because It will not let me display the code tags.
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Regex Multiple Lines
31 Aug, 2008 - 12:13 PM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,304



Thanked: 101 times
Dream Kudos: 1275
My Contributions
Have you tried using (.?*) that shopuld take any and all characters until it matches up with the closing
CODE
[/ code]
tag.
User is offlineProfile CardPM
+Quote Post

Addiction2Code
RE: Regex Multiple Lines
31 Aug, 2008 - 12:23 PM
Post #3

New D.I.C Head
*

Joined: 19 Dec, 2006
Posts: 39


My Contributions
QUOTE(BetaWar @ 31 Aug, 2008 - 01:13 PM) *

Have you tried using (.?*) that shopuld take any and all characters until it matches up with the closing
CODE
[/ code]
tag.


Well, yes, but it still is just outputting
CODE

[ code ]<br />
class Man<br />
  def initialize<br />
  end<br />
  def jump<br />

    puts "The Man Jumps!"<br />
  end<br />
end<br />
[/ code ]<br />



I'll paste the entire functions file below, maybe something else is happening that stopping this from working...

CODE

<?php

function BBCode($input)

{

    // convert [image]image_link[/image]

    $pattern[] = '/\[image\](.*?)\[\/image\]/i';

    $replace[] = '<img src="$1" border="0" alt="$1"></img>';



    // convert [url=URL]link_title[/url]

    $pattern[] = '/\[url=(.*?)\](.*?)\[\/url\]/i';

    $replace[] = '<a href="$1" target="_blank">$2</a>';



    // convert [hover=URL]Text[/hover]

    $pattern[] = '/\[hover=(.*?)\](.*?)\[\/hover\]/i';

    $replace[] = '<a onmouseover="Tip(\'$1\', WIDTH, \'40\', BGCOLOR, \'#efe0c0\', BORDERCOLOR, \'#e3c992\')" onmouseout="UnTip()">$2</a>';



    // convert [video=URL]link_title[/video]

    $pattern[] = '/\[video=(.*?)\](.*?)x(.*?)\[\/video\]/i';

    $replace[] = '<embed
            src="http://madtast.com/pages/video/player.swf"
            width="$3" height="$2" allowscriptaccess="always"
            allowfullscreen="true"
            flashvars="height=$2&width=$3&file=$1&backcolor=0xe3c992$4&stretching=uniform"
            />';



    // convert [url]url_link[/url]

    $pattern[] = '/\[url\]([.\n]*)\[\/url\]/i';

    $replace[] = '<a href="$1">$1</a>';

    // convert [ code ]code[/ code ]

    $pattern[] = '/\[ code \](.?*)\[\/ code \]/i';

    $replace[] = codize('$1');



    // convert [file=file_name]file_link[/file]

    $pattern[] = '/\[file=(.*?)\](.*?)\[\/file\]/i';

    $replace[] = '<P title="Download" STYLE="margin-bottom: 0in; margin-left: 10px; margin-right: 10px; text-indent: 24px; background: url(/css/images/emblems/file.gif) no-repeat top left;"><a href="/download.php?download_file=$1">$2</a></P>';



    // convert [new]new_text[/new]

    //$pattern[] = '/\[new\](.*?)\[\/new\]/i';

    //$replace[] = '<P title="New" STYLE="margin-bottom: 0in; text-indent: 24px; background: url(/css/images/emblems/new.gif) no-repeat top left;">$1</P>';



    // convert [idea]idea[/idea]

    //$pattern[] = '/\[idea\](.*?)\[\/idea\]/i';

    //$replace[] = '<P title="Idea" STYLE="margin-bottom: 0in; text-indent: 22px; background: #f3e7d0 url(/css/images/emblems/idea.gif) no-repeat top left;">$1</P>';



    // convert [important]!!![/important]

    //$pattern[] = '/\[important\](.*?)\[\/important\]/i';

    //$replace[] = '<P title="Important" STYLE="margin-bottom: 0in; text-indent: 24px; background: #ecb1b1 url(/css/images/emblems/important.png) no-repeat top left;">$1</P>';



    // convert [b]text[/b]

    $pattern[] = '/\[b\](.*?)\[\/b\]/i';

    $replace[] = '<b>$1</b>';


    // convert [title]text[/title]

    $pattern[] = '/\[title\](.*?)\[\/title\]/i';

    $replace[] = '<a name="$1"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>$1</b>';



    //Making Quotes Possible ["]quote["]

    //$pattern[] = '/\["](.*?)\["]/i';

    //$replace[] = '<font color="gray">"$1"</font>';



    // convert [i]text[/i]

    $pattern[] = '/\[i\](.*?)\[\/i\]/i';

    $replace[] = '<i>$1</i>';



    return preg_replace($pattern, $replace, $input);

}

function article_script($input, $article_id, $title)

{



    $date = date("m/d/Y");

    $username = $_SESSION['username'];



    if(session_is_registered(myusername))

    {

        $f_username = $username;

    }

    else

    {

        $f_username = "Guest";

    }



    //Allows Use Of HTML <center>

    $pattern[] = '<center>';

    $replace[] = '<center>';



    //Allows Use Of HTML </center>

    $pattern[] = '</center>';

    $replace[] = '</center>';



    //Allows Use Of HTML <br>

    $pattern[] = '<br>';

    $replace[] = '<br>';



    //Allows Use Of BBCode [br]

    $pattern[] = '[br]';

    $replace[] = '<br>';


    //Formal

    //$pattern[] = '%formal%';

    //$replace[] = '<P STYLE="margin-bottom: 0in; text-indent: 22px;">';



    //Quote

    //$pattern[] = '%quote%';

    //$replace[] = '<P STYLE="margin-bottom: 0in; text-indent: 22px; background: url(/css/images/emblems/quote.gif) no-repeat top left;">';



    //Normal

    //$pattern[] = '%normal%';

    //$replace[] = '<P STYLE="margin-bottom: 0in; text-indent: 22px;">';



    //Simple

    //$pattern[] = '%simple%';

    //$replace[] = '<P STYLE="margin-bottom: 0in;">';



    //End

    //$pattern[] = '%end%';

    //$replace[] = '</P>';



    //Function Replaces



    //Article ID

    $pattern[] = '%article_id%';

    $replace[] = $article_id;



    //User

    $pattern[] = '%user%';

    $replace[] = $f_username;



    //Date

    $pattern[] = '%date%';

    $replace[] = $date;



    //Add Tabing Support

    $pattern[] = '    ';

    $replace[] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';



    //Add Indenting

    $pattern[] = '[indent]';

    $replace[] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';



    $input = nl2br($input);

    $input = BBCode($input);



    //Replace RUN!

    $input = str_replace($pattern, $replace, $input);



    //Resets

    $f_username = "";



    //Add Some Styling

    $input = '

<SPAN>

    <P style="padding-left: 10px; padding-right: 10px;" title="'.$title.'">

        '.$input.'

    </P>

</SPAN>';



    //Return

    return $input;

}

function codize($input)
{
    $input = repBetween($input);

    $pattern[] = 'for';
    $replace[] = '<font class="keyword"><b>for</b></font>';

    $pattern[] = 'while';
    $replace[] = '<font class="keyword"><b>while</b></font>';

    //Note the space after loop.
    $pattern[] = 'loop ';
    $replace[] = '<font class="keyword"><b>loop</b> </font>';

    $pattern[] = 'printf';
    $replace[] = '<font class="keyword"><b>printf</b></font>';

    $pattern[] = 'print';
    $replace[] = '<font class="keyword"><b>print</b></font>';

    $pattern[] = 'return';
    $replace[] = '<font class="keyword"><b>return</b></font>';

    //Note the space after def.
    $pattern[] = 'def ';
    $replace[] = '<font class="keyword"><b>def</b> </font>';

    $pattern[] = 'if';
    $replace[] = '<font class="keyword"><b>if</b></font>';

    $pattern[] = 'elif';
    $replace[] = '<font class="keyword"><b>elif</b></font>';

    $pattern[] = 'else';
    $replace[] = '<font class="keyword"><b>else</b></font>';

    $pattern[] = '    ';
    $replace[] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';

    $pattern[] = '<br />';
    $replace[] = '</li><li bgcolor="">';

    $input = nl2br($input);

    $input = str_replace($pattern, $replace, $input);

    //Return
    return $input;
}

?>

User is offlineProfile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 1/9/09 03:07AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month