Simple reg expr...

Associate
Joined
30 Dec 2005
Posts
415
Hey to all,

Got a simple problem here.
I'm trying to read stuff in from a text file..it looks like this:

//PROCESSOR
bla bla bla bla
bla bla bla
//MEMORY
bla bla bla ba

Basically I want to be able to return all the data for the processor:

//PROCESSOR([^<>]*)//

Now that kind of works...except it returns all the processor data and everything beyond that. It's not detecting that next //

Can anyone help?

Cheers
 
What are you using, perl? Anyway, you might try escaping the forward slashes, just in case they have some special meaning, e.g. using
\/\/PROCESSOR... \/\/ etc

(not at all sure that will help btw, just a thought)
 
Here is the code if anyone wants to play:

Code:
<?php
function getinfo($url,$stat) {
		$handle = fopen($url, "rb");
		$contents = "";
		while (!feof($handle)) {
 			$contents .= fread($handle, 8192);
		}
		$regs = Array ();
		if (eregi("//$stat([^<>]*)//", $contents, $regs)) {
			$info = $regs[1];
		} 
		fclose($handle);
	return $info;
}

echo getinfo("http://hyperion.universalns.com/raw-info.php",PROCESSOR);
?>
 
Hows about this:

Code:
<?php

function getInfo($url,$stat) {
		$file = file_get_contents($url);
		$pattern = '/(?<=\/\/'.$stat.').*(?=\/\/'.$stat.')/s';
		preg_match($pattern, $file, $matches);
		return $matches[0];
}

echo htmlentities(getInfo('data.txt','PROCESSOR'));

?>

This ouputs the following:

Code:
processor	: 0
model name	: AMD Opteron(tm) Processor 240
processor	: 1
model name	: AMD Opteron(tm) Processor 240
 
Back
Top Bottom