0

Probably a simple problem here, but I cannot find it.

I am exploding a string that was input and stored from a textarea. I use nl2br() so that I can explode the string by the <br /> tag.

The string explodes properly, but when I try to get the first character of the string in a while loop, it only returns on the first line.

Note: The concept here is greentexting, so if you are familiar with that then you will see what I am trying to do. If you are not, I put a brief description below the code sample.

Code:

while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$comment = nl2br($row['comment']);
$sepcomment = explode("<br />", $comment);
$countcomment = count($sepcomment);
$i = 0;



//BEGIN GREENTEXT COLORING LOOP
while($i < $countcomment) {

$fb = $sepcomment[$i];
$z = $fb[0]; // Check to see if first character is >

if ($z == ">") {
$tcolor = "#789922";
}
else {
$tcolor = "#000000";
}

echo '<font color="' . $tcolor . '">' . $sepcomment[$i] . '</font><br>';

$i++;
}
//END GREENTEXT COLORING LOOP


}

Greentext: If the first character of the line is '>' then the color of that entire line becomes green. If not, then the color is black.

Picture: Picture of output

What I have tried:

  1. strip_tags() - Thinking that possibly the
    tags were acting as the first characters.
  2. $fb = preg_replace("/(<br\s*\/?>\s*)+/", "", $sepcomment[$i]);
  3. str_replace()
  4. echo $z //Shows the correct character on first line, blank on following lines.
  5. $z = substr($fb, 0, 1);

Here is a test I just did where I returned the first 5 characters of the string. first 5 characters

Any ideas for getting rid of those empty characters?

4

1 回答 1

1

尝试“修剪”功能

$fb = trim($sepcomment[$i]);

http://php.net/manual/en/function.trim.php

(可能是换行符,标签后面有 \n\r 个字符)

于 2013-02-19T20:54:45.263 回答