Sign in to follow this  
Followers 0
operat0r

HELP php grep ..

6 posts in this topic

I tried using preg_grep but I don't think that is what I wan't ...

I have a var $results ( a webpage ) and I want to grep the FIRST LINE with " return false" and only one IE the php equiv to

grep STRING | tail -n 1

Edited by operat0r
0

Share this post


Link to post
Share on other sites

<?PHP
//set the default timeout value
ini_set('default_socket_timeout',30);

//set the url to get
//apply urlencode() if needed
$url = "http://127.0.0.1/my_input.txt";

//get the url contents, save as string $result
//apply stream_context_create() if needed
$result = file_get_contents($url);

//split the result string in to an array, use newlines or breakrows
$result = preg_split('%[\\r\\n]+|<br */?>%', $result);

//return an array of matches
$result = preg_grep("/search_term/i",$result);

//functions
echo "<b>matches:</b>",count($result),"<br>\n";//length
echo "<b>first match:</b>",reset($result),"<br>\n";//head
echo "<b>last match:</b>",end($result),"<br>\n";//tail
echo "<b>all matches:</b>\n<ol type=1>";
foreach($result as $item){echo "\n<li>$item";}
echo "</ol>";

exit;
?>

Threw this together, might help you out, not necessarily the best way to go about things in all circumstances.

To return the first match as quickly as possible, think about using a stream_context filter and then use with preg_match/ereg, possibly tied to a return or exit statement.

You can use preg_match but it doesn't 'officially' directly support arrays as the search string and will throw a warning (yet still work), unless you iterate each item individually, flatten the array (redundant) or remove the split and add the split terms in to the string match.

Edited by jabzor
0

Share this post


Link to post
Share on other sites

"You can use preg_match but it doesn't 'officially' directly support array"

I don't want to store as an array but if I have to what ever..

basically I have a webpage in a var and want to pull a dynamic string ( tokenish var) from it ..

the code above will work but if I should be doing it a diff way let me know .. ?

working on a "Legion of the green dragon" php/curl script that will all run server side :)

I have a lame windows macro and I was going to do it in greasemonkey but I figured I should use a real lang ...

PS Thanks for the help !!

does php have a easy way to do verbose/debug mode er logging ? like "bash -x" ?

Edited by operat0r
0

Share this post


Link to post
Share on other sites

<?PHP
preg_match_all('/port: (\\d+) /si', file_get_contents("http://127.0.0.1/my_input.txt"), $result, PREG_PATTERN_ORDER);
$count=count($result[0]);
echo "<b>Head:</b> ",$result[1][0],"<br>";
echo "<b>Tail:</b> ",$result[1][count($result[1])-1],"<br>";
echo "<b>Count:</b> ",$count,"<br>";
for ($i = 0; $i < $count; $i++) {
echo "<b>string match:</b> ",$result[0][$i]," <b>capture group:</b> ",$result[1][$i],"<br>";
}
exit;
?>

The above might help you as the preg_grep was more advanced than likely needed (I use it for building csv files).

Parses an input string for all occurrences of a pattern (' port: <numbers> ') and a magic-key (return just <numbers>).

Results stored in a multidimensional array of the string that matched followed by the specific capture group info.

$result[0=stringmatch ,1=capturegroup 1,capturegroup 2, etc][0=firstmatch,1=secondmatch,2=thirdmatch,etc]

$result[0][0] = the first match string {port: 123}

$result[1][0] = the first match capture-group (head) {123}

$result[0][count($result[1])-1] = the last match string {port: 65536}

$result[1][count($result[1])-1] = the last match capture-group (tail) {65536}

Replace /port: (\\d+) / with your string match and (\\d+) with whatever the magic-key you are looking for is.

If you are *certain* that you can match the key directly the first time you could always just preg_match as Tioctsi mentioned:

<?PHP
//string match, returns search_term
if (preg_match('/sear(ch_te)rm/sim', file_get_contents("http://127.0.0.1/my_input.txt"), $storage)){
echo $storage[0];
}
//capture-group match, returns ch_te
if (preg_match('/sear(ch_te)rm/sim', file_get_contents("http://127.0.0.1/my_input.txt"), $storage)){
echo $storage[1];
}
?>

As far as debugging php, I don't exactly use php daily (unlike perl ;)) so I wouldn't be the best person to ask but:

debug_print_backtrace, the error funcs and print_r would be a good place to start.

Edited by jabzor
0

Share this post


Link to post
Share on other sites

Sweet thanks ! so far I have get/curl func and a func to parse the token :) now I just need some if loops ;)

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0