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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
06-24-2004, 03:11 PM
#1
Member
Registered: Jun 2004
Posts: 41
Rep:
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> <a href='?content=marketing&process=projects' class='subnormal'>::Projects</a></td>";
$html .= " </tr>";
}
if($this->user->inRole('marketing_developers')){
$html .= " <tr>";
$html .= " <td> <a href='?content=marketing&process=developers' class='subnormal'>::Developers</td>";
$html .= " </tr>";
}
if($this->user->inRole('marketing_handsets')){
$html .= " <tr>";
$html .= " <td> <a href='?content=marketing&process=handsets' class='subnormal'>::Handsets</td>";
$html .= " </tr>";
}
if($this->user->inRole('marketing_carriers')){
$html .= " <tr>";
$html .= " <td> <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
06-24-2004, 04:01 PM
#2
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
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" ;
}
}
?>
06-24-2004, 04:11 PM
#3
Member
Registered: Jun 2004
Posts: 41
Original Poster
Rep:
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
06-24-2004, 04:14 PM
#4
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
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 .
06-26-2004, 07:37 AM
#5
Member
Registered: Jun 2004
Posts: 43
Rep:
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!
06-26-2004, 08:12 AM
#6
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
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 .
06-26-2004, 08:36 AM
#7
Member
Registered: Jun 2004
Posts: 43
Rep:
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?
06-26-2004, 12:19 PM
#8
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
Yes you guess it right.
^: search from the beginning of line
. : any character match
* : search previous 0 times or more
06-26-2004, 02:04 PM
#9
Member
Registered: Jun 2004
Posts: 41
Original Poster
Rep:
Just look up regular expressions cuz man are the fun and so useful
06-26-2004, 05:01 PM
#10
Member
Registered: Jun 2004
Posts: 43
Rep:
They have 'Hooked on Phonics' program that is as hot as the Reality TV, so why don't we have 'Hooked on ReGex'?
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 06:42 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News