LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Php and regular expressions (https://www.linuxquestions.org/questions/programming-9/php-and-regular-expressions-197295/)

logicdisaster 06-24-2004 03:11 PM

Php and regular expressions
 
Okay kinda stumped here i have a php file that has code like:
if($this->user->inRole('marketing')){
$html .= " <tr>";
$html .= " <td><a href='#' onClick='show(\"marketing\")' class='normal'>::Marketing</a>";
$html .= " <div id='marketing' style='display:none;'>";
$html .= " <table cellspacing='0' border='0' width='150' cellpadding='0'>";
if($this->user->inRole('marketing_projects')){
$html .= " <tr>";
$html .= " <td>&nbsp;&nbsp;&nbsp;<a href='?content=marketing&process=projects' class='subnormal'>::Projects</a></td>";
$html .= " </tr>";
}
if($this->user->inRole('marketing_developers')){
$html .= " <tr>";
$html .= " <td>&nbsp;&nbsp;&nbsp;<a href='?content=marketing&process=developers' class='subnormal'>::Developers</td>";
$html .= " </tr>";
}
if($this->user->inRole('marketing_handsets')){
$html .= " <tr>";
$html .= " <td>&nbsp;&nbsp;&nbsp;<a href='?content=marketing&process=handsets' class='subnormal'>::Handsets</td>";
$html .= " </tr>";
}
if($this->user->inRole('marketing_carriers')){
$html .= " <tr>";
$html .= " <td>&nbsp;&nbsp;&nbsp;<a href='?content=marketing&process=carriers' class='subnormal'>::Carriers</td>";
$html .= " </tr>";
}
$html .= " </table>";
$html .= " </div>";
$html .= " </td>";
$html .= " </tr>";
}

i need to write a parser that will open the php file and find the inRoles. Pretty much it would need to open it find each inRole('name') and copy the name into a file so i would end up with

marketing
marketing_projects
marketing_developers
marketing_handsets
marketing_carriers

in another text file

im just not sure how to set up the regular expressions to do this or which reg-ex function to use

keefaz 06-24-2004 04:01 PM

PHP Code:

<?php
$lines 
file"your_file.php" );

for( 
$i 0$i sizeof($lines); $i++ )
{
    if (
preg_match("/inRole/"$lines[$i]) )
    {
        
$str ereg_replace"^(.*)\('"""$lines[$i] );
        
$str ereg_replace"'(.*)$"""$str );
        echo 
"$str\n";
    }
}
?>


logicdisaster 06-24-2004 04:11 PM

Warning: ereg_replace(): REG_EPAREN: in c:\inetpub\wwwroot\mobile_production\version 0.2\roles.php on line 8

Warning: ereg_replace(): REG_EPAREN:eparentheses not balanced in c:\inetpub\wwwroot\mobile_production\version 0.2\roles.php on line 8

i get those two errors when i run the script

keefaz 06-24-2004 04:14 PM

add a \ in
PHP Code:

$str ereg_replace"^(.*)\\('"""$lines[$i] ); 

I already put the \ in the first code but to display it in this forum I have to use \\ instead ;)

cpanelskindepot 06-26-2004 07:37 AM

OK keefaz,

Lets make life a little harder for you....;)
What if there are 500 files like the one in first post.
What do we do to read all files in the same directory and output the result?
Is it possible? Surprise me!

keefaz 06-26-2004 08:12 AM

yeah it is possible ;)

Assuming directory contains just your php file to parse,
PHP Code:

<?php
$dir   
"/path/to/your/500files/directory/";

$d opendir$dir );
while ( (
$file readdir($d)) != false )
{
    
$lines file$file );

    for( 
$i 0$i sizeof($lines); $i++ )
    {
        if (
preg_match("/inRole/"$lines[$i]) )
        {
            
$str ereg_replace"^(.*)\\('"""$lines[$i] );
            
$str ereg_replace"'(.*)$"""$str );
            echo 
"$str\n";
        }
    } 
}
closedir($d);
?>


cpanelskindepot 06-26-2004 08:36 AM

Super cool. Care to explain What this line means?
$str = ereg_replace( "^(.*)\('", "", $lines[$i] );

Let me guess.
In english it would be Get rid of anything before ('
Right?

keefaz 06-26-2004 12:19 PM

Yes you guess it right.

^: search from the beginning of line
. : any character match
* : search previous 0 times or more

logicdisaster 06-26-2004 02:04 PM

Just look up regular expressions cuz man are the fun and so useful

cpanelskindepot 06-26-2004 05:01 PM

They have 'Hooked on Phonics' program that is as hot as the Reality TV, so why don't we have 'Hooked on ReGex'? ;)


All times are GMT -5. The time now is 07:42 PM.