Jump to content

[SOLVED] For each character in string...


Asday

Recommended Posts

Right, I have a set of code that produces a number.

 

I want to display those numbers as images.

 

I know how to make the images appear.

 

I need to know how to run a piece of code for every number in the string, on each number in the string, such as (VB.NET ish):

 

For Each char As Character in $count194

char(x)

offset = x

//  echo the image (don't worry about this bit, just put a comment like this where it should be)

Next

 

Link to comment
https://forums.phpfreaks.com/topic/58718-solved-for-each-character-in-string/
Share on other sites

<?php 
$mystring = 'foobar';
for($x=0;$x<strlen($mystring);$x++){
echo $mystring[$x].'<br />';

}
?>

 

K, tried that, (with my code, which produces a "1") and the page source was:

 

<br />

 

My code is this:

 

<?php
$fp=fopen("count.txt", "r+b");
$count194=fread($fp, filesize("count.txt"));
fclose($fp);
unlink("count.txt");
$count194++;

for($x=0;$x<strlen($count194);$x++)
{
echo $count194[$x] . "<br />";
}

// echo $count194;

touch("count.txt");
$fpNew=fopen("count.txt", "r+b");
fwrite($fpNew, $count194);
?>

$count194++;

 

Why are you adding one to a "string". Thats why it produces "1".

 

It's meant to produce "1".  The next time it's run, it produces a "2".  It's a counter.  The original "string" is "0".  That's not the problem.  The problem is that that code sample only outputs

<br />

Int's are different than strings.

 

Try this to convert that int to a string:

 

$count194++;
$count194 = "$count194";

for($x=0;$x<strlen($count194);$x++)
{
echo $count194[$x] . "<br />";
}

 

See if that works.

 

Excellent, that works perfectly.  I now have images instead of numbers.  (^-^ )

 

Thanks, all!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.