searching a string [SOLVED] =)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

searching a string [SOLVED] =)

Post 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
Last edited by d3ad1ysp0rk on Mon Feb 02, 2004 5:22 pm, edited 1 time in total.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Are you parsing XML without the DOM extensions?
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

with strpos you can find position of '<hits>' and position of '</hits>'
with substr you can find the substring between the 2.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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;
}
?>
Post Reply