Jump to content

grep-ing with a php script


PhilTR

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.