LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-24-2004, 03:11 PM   #1
logicdisaster
Member
 
Registered: Jun 2004
Posts: 41

Rep: Reputation: 15
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
 
Old 06-24-2004, 04:01 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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";
    }
}
?>
 
Old 06-24-2004, 04:11 PM   #3
logicdisaster
Member
 
Registered: Jun 2004
Posts: 41

Original Poster
Rep: Reputation: 15
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
 
Old 06-24-2004, 04:14 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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

Last edited by keefaz; 06-24-2004 at 04:16 PM.
 
Old 06-26-2004, 07:37 AM   #5
cpanelskindepot
Member
 
Registered: Jun 2004
Posts: 43

Rep: Reputation: 15
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!
 
Old 06-26-2004, 08:12 AM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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);
?>

Last edited by keefaz; 06-26-2004 at 08:14 AM.
 
Old 06-26-2004, 08:36 AM   #7
cpanelskindepot
Member
 
Registered: Jun 2004
Posts: 43

Rep: Reputation: 15
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?
 
Old 06-26-2004, 12:19 PM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Yes you guess it right.

^: search from the beginning of line
. : any character match
* : search previous 0 times or more
 
Old 06-26-2004, 02:04 PM   #9
logicdisaster
Member
 
Registered: Jun 2004
Posts: 41

Original Poster
Rep: Reputation: 15
Just look up regular expressions cuz man are the fun and so useful
 
Old 06-26-2004, 05:01 PM   #10
cpanelskindepot
Member
 
Registered: Jun 2004
Posts: 43

Rep: Reputation: 15
They have 'Hooked on Phonics' program that is as hot as the Reality TV, so why don't we have 'Hooked on ReGex'?
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expressions in php ALInux Programming 4 11-07-2005 11:48 AM
Regular Expressions markjuggles Programming 2 05-05-2005 11:39 AM
Regular Expressions overbored Linux - Software 3 06-24-2004 02:34 PM
Regular expressions aromes Linux - General 1 10-15-2003 12:29 PM
regular expressions? alaios Linux - General 2 06-11-2003 03:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration