View Full Version : while help

sir pannels
01-09-2006, 10:38 AM
Hi all,

Having some problems with a while loop, not to sure how to go about what I want.
Checked out php.net and some online tutorials but still struggiling slightly.

What I have is a few lines of code that takes some information for a file and puts it into variables then echo's the values at the end of the script.
The script takes the data out of the file by reading the values between certain markers in the file such as <webversion>VALUE</webversion>.
That works great however <webversion>VALUE</webversion> will appear in the file either 16 or 24 times with a different value.
I'm trying to wrirte a loop to cycle through each ocurence of <webversion>VALUE</webversion> and print it each time it is there.
The while I have at the moment does it 24 times however only prints the first ocurence of webversion value, 24 times.

Can anyone offer advice on where I'm going wrong? Loops have never been my strong point.

Many thanks,

Here's the code I have that prints the same value 24 times:

$fh = fopen($file,"r");
while($data = fgets($fh)) {

$i = 0;
while ($i <= 24):

#### General Information Gathering & Display - This below actualy works.
$posOpen = strpos($d, "<webversion>") + 12;
$posClose = strpos($d, "</webversion>");
$webversion = substr($d, $posOpen, $posClose - $posOpen);

$posOpen = strpos($d, "<version>") + 9;
$posClose = strpos($d, "</version>");
$version = substr($d, $posOpen, $posClose - $posOpen);

$posOpen = strpos($d, "<sitename>") + 10;
$posClose = strpos($d, "</sitename>");
$sitename = substr($d, $posOpen, $posClose - $posOpen);
#### End of gathering

echo ("Web Version $webversion of Version $version for site $sitename.<br>");

01-09-2006, 10:42 AM
strpos() returns the position of the first thing found. You'd have to set strpos()'s offset after the first elemnt if you'd want to do it your way.

sir pannels
01-09-2006, 11:43 AM
I see, thanks for reply.

That could get messy then. You know of any better ways to do what im going for?


01-09-2006, 12:28 PM
delimiters ! , without them, e.g some way of knowing where 1 `record` starts and finishes within the text file all methods are going to be prone to error, this should work but only with correctly grouped bunches of the three markers you are looking for (e.g. a missing <version></version> and this will give erroneous data...

$yaks = file_get_contents($file);


//this is dodgy but anyway ~ you get the general idea
foreach($regs[0] as $k=>$arr){
echo "Web Version {$regs[1][$k]} of Version {$regs[2][$k]} for site {$regs[3][$k]}.<br />";

If you could say that each group of text containing the markers had some sort of delimiter ... say '|' or a couple of newlines .. or anything unique then we could explode() the text input by that delimiter and get safer results (and we could then lose preg_functions and use strpos etc as you did above.

01-09-2006, 12:46 PM
You may want to use *? instead of * in some places but I'd have to test it to be sure.

sir pannels
01-11-2006, 09:50 AM
Thanks both of you.

Played with your function firepages but only get undefined call to function, which is odd as Zend does not see a problem. What am I doing wrong? I can't see how its undefined ... any further help would be apperciated :)


01-11-2006, 02:15 PM
Which function?
Zend debugger uses it's internal PHP version (by default) and it may be newer than the one on your test server.

sir pannels
01-11-2006, 03:25 PM
hmm yeah that is possible .. file_get_contents($file); is what is giving me the problem..

01-11-2006, 04:35 PM
....file_get_contents($file); is what is giving me the problem..

ok, must be an older version of PHP... try

$yaks = implode('',file($file));


sir pannels
01-11-2006, 05:27 PM
aye must have been older version, got that working great.

Few problems with the array vars being inside {}... for example..

if($state=='1'){ $bg='#FFFF99'; $value='ON'; }
if($readystate=='1'){$newstate = 'on';}

the above, gives me a error on the = on the last line, it is expecting a } random, I have checked and all the brackets balance. I removed the brackets round state .. so
$state='$regs[3][$k]'; and it did not error on the = 'on' but obviously just displays Array[x].

What have I done wrong here? Many thanks again firepages =]

01-11-2006, 06:09 PM
Remove the single-quotes. It will not evaulate in single-quotes.

01-11-2006, 06:22 PM
Hi , the curly brackets are only needed for echoing within a string ...
e.g this will work
echo "use an array in a string = {$array['blah']['blah']} ";
this would error (or give the wrong result)
echo "use an array in a string = $array['blah']['blah'] ";
to simply echo or use the variable elsewhere...
echo $array['blah']['blah'];

so lose those and the quotes as marek_mar mentions

$newstate = 'on';

sir pannels
01-12-2006, 10:01 AM
ah, great yeah they display now. Thanks.

Something I don't understand about your code, what does the leading / in the pregmatch do or look for and same question to /Uis? Just so I have a better undestanding of whats going on.

Got another problem that the code only prints everyother entry, it will bring back one lot of details, skip the next and then bring back the others.. like 1,3,5,7 and so forth. Maybe I will be able to fix this is I understand the /Uis and stuff :)

cheers Fire and M, you are both very helpful.

01-12-2006, 10:13 AM
the /'s are delimiters so the regular expression pattern goes in between those (they dont have to be slashes they can be pipes | etc)

after the final slash are the modifiers
U = ungreedy this should stop the regex from matching everything between the first <webversion> and the last <sitename> in the file , e.g. it looks for multiple instances of the same pattern
i = case insensitive search
s = ...errr had to look this up :) at http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php have a looksee it explains all the modifiers

why you are getting every other instance I really don't know without seeing the contents of $file, its a hit and miss affair relying on regex to parse this type of data , thats why some form of `record` delimiter would be useful.