Forum Moderators: coopster

Message Too Old, No Replies

Search string inside html file

how to....

         

chocorol

12:26 am on Dec 27, 2007 (gmt 0)

10+ Year Member



Hope somebody can help me with this: What I want to do is to search inside a .html file for a specific string: "number=#*$!#*$!#*" where the X's are 10 numbers that change all the time.

The file is stored in a tmp folder, so I will use fopen but then, what function can I use so that I can store the X's numbers in $numbers variable and then delete the file?

Edit: I see the X's were replaced with #*$!#*$!#*

Habtom

4:39 am on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



regular expression seems to be the way forward.

PHP_Chimp

10:19 am on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just encase you haven't already sorted out the regex -

[url=http://uk.php.net/manual/en/function.preg-match.php]preg_match[/url]('%number=(\d){10}%', $input, $match)

This will match 10 decimal digits after number =, and put the digits in $match[1]. You may need to be careful of white space, as at the moment if there are any spaces between number , = or the digits then this expression will not match. So you may need to do something like -

'%number\s?=\s?(\d){10}%i'

The i will also make number case insensitive, so it will accept NUMBER or Number as well. It all depends on how much control you have over the formatting of this string. If you are going for user supplied input then you need to consider that they will enter information with the caps lock on, capitalize things or put spaces/tabs where you dont want them to. You may even need to change the? to + to allow for people to put multiple space characters in, as at the moment any single white space character is fine, but 2 space characters would break the regex.

chocorol

11:17 pm on Dec 27, 2007 (gmt 0)

10+ Year Member



Thanks a lot for your answers. Yes, I had to use regular expressions. What I got after several tries was the following:


$file = '/home/something/else/filetmp';
$handle = fopen($file, "r");
$input = fread($handle, filesize($file));
fclose($handle);

$pattern = '[0-9]{10}';
ereg($pattern,$input,$regs);
$session = $regs[0]; // Now I have the 10 digits number stored in $session

Can you spot any mistake in the code? Is fclose() in the right location so that the file is indeed closed?

The code does work as I expected, but maybe it's more efficient to use the method PHP_Chimp described, with preg_match() so I will be testing it. Thanks again for your answers.

Edit: used [ code ] tag for code formatting

PHP_Chimp

11:39 pm on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All of the file block looks great.

ereg is slower than preg_*. So generally there is no advantage to using ereg over something like preg_match. However the speed difference with most of these things is fractions of a millisecond...so faster may well not be something that you actually notice.

The biggest advantage of preg_* is that it is PERL compatible regular expressions, so there is a lot more you can do with preg than you can with ereg. Like the fact you can use the pre-built character classes, like \d ;)