Current location: Hot Scripts Forums » Programming Languages » PHP » Regular Expression help


Regular Expression help

Reply
  #1 (permalink)  
Old 11-30-05, 07:46 PM
zoic zoic is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy Regular Expression help

I have been trying and trying but can't seem to get my regular expression to work like I want it too

I'm working on a template system for my forum and having the ability to put conditional statements within the template in the following format:

<if condition="$a == $b">
...
<else>
...
</if>

My problem is with the code that I have right now (which is extremely messy), I can't put one inside of another. I got to a point where I really need to do this for my forum now. Since I had recently learned the basics of regular expressions and have messed around with them for a bit I decided to try it that way and scrap what I had before.

This is what I have right now:

PHP Code:

<?php


function parseConditionals$string )
{
$string addslashes$string );

$string eregi_replace'<if condition=\"([.]*)\">''".(( \\1 ) ? ("'$string ); // need help with this part
$string str_replace'<else>''") : ("'$string );
$string str_replace'</if>''"))."'$string );

eval( 
'$string ="'.$string.'";' );

return 
stripslashes$string );
}

?>
Of course the <else> and </if> were easy, just a simple str_replace, but the first one is what I'm having a problem with. I have a decent understanding of regular expressions but have tried for a couple hours with no success.

If anyone could help me out with this I'd appreciate it greatly.
__________________
Zoic
"There's no place like 127.0.0.1"
KrypTek.org
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 12-01-05, 08:28 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
PHP Code:

<?php

$string 
= <<<STR
<if condition="\$a == \$b">
...
<else>
...
</if>
STR;

preg_match('~<if condition="(.+)">(.*)<else>(.*)<\/if>~is'$string$arr);

unset(
$arr[0]);

$string "if ($arr[1]) {
   
$arr[2]
} else {
   
$arr[3]
}"
;

echo 
$string;
?>
and to make in that function:
PHP Code:

function parseConditionals$string )

{
   
$string addslashes$string );

   
preg_match('~<if condition="(.+)">(.*)<else>(.*)<\/if>~is'$string$arr);

   unset(
$arr[0]);

   
$string "if ($arr[1]) {
   
$arr[2]
} else {
   
$arr[3]
}"
;

   return 
$string;

__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 12-01-05, 11:05 AM
zoic zoic is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
That seems to be able to work, but for only just one conditional statement..I need it to support multiple statements and one within the other like this:

Code:
<if condtion="$a==$b">

     <if condition="$c==$d">

     <else>

     </if>
<else>

     <if condition="$e==$f">

     <else>

     </if>

</if>
That's why I used the str_replace and eregi_replace before...

Thanks for your reply though
__________________
Zoic
"There's no place like 127.0.0.1"
KrypTek.org
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 12-01-05, 03:08 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
oh... here comes a recursive function
I will look into that tomorrow.. time to sleep..
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 12-02-05, 07:15 PM
zoic zoic is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
No need anymore, I figured it out by tweaking my other code...

PHP Code:

<?php


    
function parseConditionals$string )
    {
        
$string addslashes$string );

        
$if '<if condition=\"';
        
$string str_replace'<else>''" : "'$string );
        
$string str_replace'</if>''")."'$string );

        
$if_first strpos$string$if );
        
$num_ifs substr_count$string$if );

        for ( 
$i 0$i $num_ifs$i++ )
        {
            if ( 
$i == )
            {
                
$if_start $if_first;
            }
            else
            {
                
$if_start strpos$string$if, ( $if_start ) );
            }

            
$condition_start = ( $if_start strlen$if ) );
            
$condition_end strpos$string'\">', ( $if_start ) );

            
$if_end = ( $condition_end );
            
$if_len = ( $if_end $if_start );

            
$condition_value stripslashessubstr$string$condition_start, ( $condition_end $condition_start ) ) );
            
$end_start strpos$string$endif, ( $if_start $if_len ) );

            
$string substr_replace$string'".('.$condition_value.' ? "'$if_start$if_len );
        }

        eval( 
'$string = "'.$string.'";' );

        return 
stripslashes$string );
    }

?>

Thanks for your help though
__________________
Zoic
"There's no place like 127.0.0.1"
KrypTek.org
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
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
javascript regular expression chet JavaScript 0 08-19-05 03:47 PM
regular expression for parsing url link musmanm80 Perl 1 07-28-05 12:58 AM
Regular Expression KHWright Visual Basic 0 06-02-05 07:40 PM
I want a crowler and abstraction from Regular Expression oiranoinu Script Requests 4 11-26-04 12:31 AM
suggest a regular expression gmadhukarreddy Perl 2 01-08-04 01:56 AM


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