PhilTR Posted January 4, 2008 Share Posted January 4, 2008 I'm trying to grep a string out of a *.php file using a php script storing the string in a variable. Is this even possible? If so what would the command look like? I'm trying to pull the substring (everything between parenthesis): "/var/www/html/$newAccountNo", 0775 from the string: $result_dir=mkdir("/var/www/html/$newAccountNo ", 0775); located (naturally) in 'plmNewAcctData.php'. I can grep it with: 'grep -i '$result_dir=*' plmNewAcctData.php'. I'm doing this as a learning exercise and of course I'm a bit new at this so if it's possible please show me what the code would looks like using the info I provided. Hopefully I have provided enough info. Thanks for your help. phil Quote Link to comment https://forums.phpfreaks.com/topic/84390-grep-ing-with-a-php-script/ Share on other sites More sharing options...
hitman6003 Posted January 4, 2008 Share Posted January 4, 2008 use preg_match: //not tested: if (preg_match('/\$result=mkdir\((.*?)\).*/i', file_get_contents(plmNewAcctData.php), $matches)) { print_r($matches); } Quote Link to comment https://forums.phpfreaks.com/topic/84390-grep-ing-with-a-php-script/#findComment-429907 Share on other sites More sharing options...
PhilTR Posted January 4, 2008 Author Share Posted January 4, 2008 guru, thanks for the code snippit. Laying in bed I realized I didn't completely make myself clear. I edited my original post after your reply. I needed to make clear that I wanted to store the string in a variable. I should have much fun playing with your code. The string looks like it's stored in '$matches' ? Thanks. phil Quote Link to comment https://forums.phpfreaks.com/topic/84390-grep-ing-with-a-php-script/#findComment-430028 Share on other sites More sharing options...
rajivgonsalves Posted January 4, 2008 Share Posted January 4, 2008 Well $matches is an array of all the matches in the file... you can move through the array and handle each match the way you want to Quote Link to comment https://forums.phpfreaks.com/topic/84390-grep-ing-with-a-php-script/#findComment-430030 Share on other sites More sharing options...
PhilTR Posted January 4, 2008 Author Share Posted January 4, 2008 Not having much luck. I think I found a typo in Guru's example tho. The first part of the string I want to pull from is, '$result_dir=mkdir...' and not '$result=mkdir...'. (Actually all I want to pull is the text between the ()s, including the leading double quote (for now the substring is unique). I'll store the var $matches in another var called '$dir_path' and call '$dir_path' from within 'plmNewAcctData.php'(using 'return $matches')). So, I edited the example and tried again, w/o success. I tried another uniquie comment string from same file thinking that simpler is better. <code>if (preg_match('/\bcheck for missing field entrys\b/i', file_get_contents(plmNewAcctData.php), $matches)) { print_r($matches); } </code> Nada. I even tossed in: <code> $handle = fopen("/var/www/html/plm/plmNewAcctData.php", "r"); </code> to see if that made 'plmNewAcctData.php' open and readable. I'm wondering if 'file_get_contents' isn't the problem needing modifiers. So I skimmed the manual for 'file_get_contents' too. Not sure any modifiers are appropriate here. At least none jumped out at me. I even checked my php versions (I have PHP 5.1.6 (cli) (built: Sep 18 2007 09:07:04). So it should handle all version changes short of 6. The files 'plmNewAcctData.php and 'findString.php' are owned by 'apache' and are executable (I even tried changing ownership of dir 'plm' to apache too)(As an aside, I have another php script called 'sringtotime.php (<code> $timestamp=strtotime("now"); return $timestamp; </code> owned by 'root' and not executable that I call from within 'plmNewAcctData.php that creates a unix timestamp with which I create a a unique account # (and dir) <code> $stt=include 'strtotime.php'; </code>) <code> $ins_stt="UPDATE sellersAccts SET accountNumber='$stt' WHERE userName='$_POST[userName]'"; </code> under '/var/www/html'). Also, I'm running FedoraCore6. Both php scripts are in the same directory ('/var/www/html/plm/') so php should be able to find the target file. As noted above, I've studied 'the manual' but, some of the conventions are greek to me, eg., '@^(?:http://)?([^/]+)@i'. Not the foggiest what @, ^,? and + signify and how properly used. Also boning up on fopen(). Finally, running 'php /var/www/html/plm/findString.php' on my above 'simpler' example gets: [root@philsfc6 plm]# php /var/www/html/plm/findString.php PHP Notice: Use of undefined constant plmNewAcctData - assumed 'plmNewAcctData' in /var/www/html/plm/findString.php on line 3 PHP Notice: Use of undefined constant php - assumed 'php' in /var/www/html/plm/findString.php on line 3 PHP Warning: file_get_contents(plmNewAcctDataphp): failed to open stream: No such file or directory in /var/www/html/plm/findString.php on line 3 Is seem as if php does not see 'plmNewAcctData.php' as a file. In the file 'gedit' paints the 'dot' brown as it would the word 'echo'. I appreciate your patience. phil Quote Link to comment https://forums.phpfreaks.com/topic/84390-grep-ing-with-a-php-script/#findComment-430486 Share on other sites More sharing options...
PhilTR Posted January 7, 2008 Author Share Posted January 7, 2008 Placing <plmNewAcctData.php> in double quotes in: <code>if (preg_match('/\bcheck for missing field entrys\b/i', file_get_contents(plmNewAcctData.php), $matches)) { print_r($matches); } </code> produces the output, "Array". Anyone know what this means? And, why would php see the file name as an 'undefined constant' and what does 'failed to open stream' mean? Any help appreciated. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/84390-grep-ing-with-a-php-script/#findComment-432842 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.