...

View Full Version : First Character Not Always?



dniwebdesign
04-26-2006, 07:55 AM
for($i=0;$i<$counter;$i++)
{
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?

devinemke
04-26-2006, 05: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)):


<?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.

dniwebdesign
04-26-2006, 10: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.

dniwebdesign
04-27-2006, 12:20 AM
Here:
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.

Fumigator
04-27-2006, 12: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.

dniwebdesign
04-27-2006, 01: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:
http://www.dni-server.no-ip.com/allstar/index.phps

Current Output:
http://www.dni-server.no-ip.com/allstar/index.php

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.

khendar
04-27-2006, 02: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.

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum