View Full Version : First Character Not Always?

04-26-2006, 08:55 AM
if($i!=($counter-1)) {
$music = explode(" - ",$theexplode[$i]);
if($music[2]=="m") {
$fileType = "MP3";
elseif($music[2]=="s") {
$fileType = "SID";
elseif($music[2]=="o") {
$fileType = "OGG";
elseif($music[2]=="w") {
$fileType = "WAV";

echo $i." ";
$theArtist = $music[0];
echo $theArtist{0}."<br>";

//echo "<tr onMouseOver=\"this.style.background='#F0F0F0'\" onMouseOut=\"this.style.background='#FFFFFF'\"><td class='leftcell'>".($i+1)."</td><td class='regcell'>".$music[0]."</td><td class='regcell'>".$music[1]."</td><td class='rightcell'>".$fileType."</td></tr>";


I have the above for loop which gets info from an array.
Just ignore the last commented table row (it works fine). What I am having problems with is statement before it. It is only getting the character from the first loop. After it loops once it will not get the character for the rest of the time it's looping and I don't know why. What am I doing wrong?

04-26-2006, 06:18 PM
the curly brace syntax "{}" immediately following a string is used to return specific characters within that sting. see this example (lifted directly from the manual page on strings (http://www.php.net/manual/en/language.types.string.php)):

// Get the first character of a string
$str = 'This is a test.';
$first = $str{0};

// Get the third character of a string
$third = $str{2};

// Get the last character of a string.
$str = 'This is still a test.';
$last = $str{strlen($str)-1};

// Modify the last character of a string
$str = 'Look at the sea';
$str{strlen($str)-1} = 'e';

anyway in your line of code here:

echo $theArtist{0}."<br>";
you are echoing only the first character of $theArtist.

the broader issue here is that you seem to be overcomplicating this whole process. post an example of what you array looks like.

04-26-2006, 11:46 PM
That is what I want... I want to echo the first character of $theArtist however it will only echo it on the first loop (of the for loop). After it does the loop once it doesn't echo it.

04-27-2006, 01:20 AM
The array consists of an artist, title and format as it goes through the array it explodes it into that array.
So for example...

$theArray -> {
[0] = Elvis
[1] = Love Me Tender
[2] = m

then the next loop it would be something like:

$theArray -> {
[0] = Elvis
[1] = Heartbreak Hotel
[2] = m

However I would only get the "E" on the first loop. It will not get it for any loop after that, if I am making myself clear.

04-27-2006, 01:50 AM
Your code works, unless there are only two entries in the $theexplode array, due to the questionable if statment inside the for loop, which is bypassing the entire code block on the last array element.

Instead of the if statement, modify the for loop to stop processing one iteration early, i.e.

for($i = 0; $i < $counter - 1; $i++)

I'd also suggest a switch statement for the $music[2] element, rather than nested if statements. It's just more better.

04-27-2006, 02:19 AM
Well... depsite your suggestions it doesn't really solve the question at hand now, does it? What I'm trying to do is get the first character of the artist's name, and as you said it works. Except it doesn't, which is why I'm so confused. So here we go.

Here is my PHP file:

Current Output:

And I know my html is out of place and stuff, I'm just trying to get it to do what I want to do at the moment and worry about formatting later.

04-27-2006, 03:11 AM
OK your problem is that your artist name value has a new line character at the start. Your code is thinking that this new line character is the first character of the name so it prints out that instead of the letter.

You'll need to remove the new line character before echoing the first character of the artists name. Using something like:

$theArtist = preg_replace("/[\r\n]+[\s\t]*[\r\n]+/", "\n", $music[0]);
$theArtist = preg_replace("/^[\s\t]*[\r\n]+/", "", $theArtist);
echo $theArtist{0}."<br>";

Or better yet - try to remove it from the music array as it gets read in.

04-27-2006, 03:58 AM
Thanks... works like a charm. ;-)