Page 1 of 1

searching a string [SOLVED] =)

Posted: Sun Feb 01, 2004 2:47 pm
by d3ad1ysp0rk
I want to find whats between 2 things, ie:

<hits>588</hits>

is in my text file, and i want to get '588'.

i looked through the function list ( http://us4.php.net/manual/en/ref.strings.php ) but i cant find what im looking for..


thanks

Posted: Sun Feb 01, 2004 3:20 pm
by patrikG
Are you parsing XML without the DOM extensions?

Posted: Sun Feb 01, 2004 7:13 pm
by d3ad1ysp0rk
nope, i just used <hits> as an example.

it could be :hits: for all i care, or even *$@#588$&*#, i just want to access a specific part of my text file.

Posted: Mon Feb 02, 2004 12:41 am
by timvw
with strpos you can find position of '<hits>' and position of '</hits>'
with substr you can find the substring between the 2.

Posted: Mon Feb 02, 2004 5:21 pm
by d3ad1ysp0rk
THANK YOU for your help!
Here's what I came up with incase anyone else needed something like this:

Code: Select all

<?php
function findinfile($filename,$start,$end){
$handle = fopen($filename, "a+");
$string = fread($handle, filesize($filename));
$pos1 = strpos($string,$start);
$pos2 = strpos($string,$end);
$pos1 = $pos1 + 6;
$length = $pos2 - $pos1;
$val = substr($string, $pos1, $length);
return $val;
}
?>