Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Can't reach a character in a string

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Can't reach a character in a string

Postby wolfmah » Sat Dec 14, 2002 11:41 am

I'm trying to retreive the first character of a string which is in an array but I can only reach the first char of only the first element.
Code: Select all
$bands= "";
$file= fopen("bands.txt", "r");
while (!feof ($file)) { $bands.= fgets($file); }
fclose ($file);

$bandsArray= explode("/*****/", $bands);

for ($i= 0; $i < count($bandsArray); ++$i)
   {
   echo($bandsArray[$i]{0});
   }

In this example, the array is containing 9 elements that can be all seen if I don't put the {0} in the echo function. The problem is that I only see the first char of only the first element.

Also, if I declare my array with explicit content like this
Code: Select all
$bandsArray= array("Ab", "Bc", "Cd", "De", "Ef", "Fg", "Gh", "Hi", "Ij");
and use the same for statement, I see all first char of the 9 elements.

Why is it doing this?
wolfmah
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sat Dec 14, 2002 11:27 am

Postby Joan Garnet » Fri Dec 20, 2002 7:21 pm

I've used another method:
Code: Select all
<?php
$bands= ("tindersticks-scott walker-plaid-pluxus-the residents");
$bandsArray= explode("-", $bands);
for ($i= 0; $i < sizeof($bandsArray); $i++){
   echo substr( $bandsArray[$i] , 0 , 1 )."<br>";
   }
?>

But I know there are those Perl based functions preg_replace that work perfect although I don't know them...

;)
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Postby wolfmah » Fri Dec 20, 2002 9:36 pm

I found what my problem was, I was able to reach the first character, but the first character was a carriage return. Now, I do a trim() before accessing them and everything works fine.
wolfmah
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sat Dec 14, 2002 11:27 am


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 5 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron