Friday, 15 June 2012

PHP: Reading first character of exploded string in while loop. Empty characters causing issues -



PHP: Reading first character of exploded string in while loop. Empty characters causing issues -

probably simple problem here, cannot find it.

i exploding string input , stored textarea. utilize nl2br() can explode string <br /> tag.

the string explodes properly, when seek first character of string in while loop, returns on first line.

note: concept here greentexting, if familiar see trying do. if not, set brief description below 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 see if first character > if ($z == ">") { $tcolor = "#789922"; } else { $tcolor = "#000000"; } echo '<font color="' . $tcolor . '">' . $sepcomment[$i] . '</font><br>'; $i++; } //end greentext coloring loop }

greentext: if first character of line '>' color of entire line becomes green. if not, color black.

picture: what have tried:

strip_tags() - thinking perchance tags acting first characters. $fb = preg_replace("/(<br\s*\/?>\s*)+/", "", $sepcomment[$i]); str_replace() echo $z //shows right character on first line, blank on next lines. $z = substr($fb, 0, 1);

here test did returned first 5 characters of string.

any ideas getting rid of empty characters?

try "trim" function

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

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

(probably line breaks problem, there \n\r characters after tag)

php

No comments:

Post a Comment