Search code examples
phpmysqlfor-loopcharacterstrip

Strip character in a php 'for' loop/mysql query


I have a loop that successfully shows account numbers. Some of these numbers were saved into the db with an "@" prepended. I would like to strip the "@" sign from the displayed results. I've seen plenty of solutions for a simple echo that accomplishes this by using trim, preg_replace, etc, but nothing within a for loop. Any ideas?

My current code (not including an attempted solution):

<?php
$query = sprintf("SELECT banner_id FROM tablename") or die(mysql_error());
$result = mysql_query($query, $dblink) or die(mysql_error());
$i = 0;
if($result){
  while($row = mysql_fetch_array($result)){
    $banner_id[$i] = $row['banner_id'];
    $i++;
  }
}
?>
<html><body>
<ul>
<?php     
    for($x=0; $x < mysql_num_rows($result); $x++) {
        echo "<li>{$banner_id[$x]}</li>\n";
    }
?>
</ul>
</body></html>

Current Output:

  • 12345678
  • @98765432
  • @01230145

Desired Output:

  • 12345678
  • 98765432
  • 01230145

I feel like this should be easy, but I can't find anything that works. Thanks for any help you can offer!


Solution

  • I can't get why you think you can't use trim() inside a for loop.

    <?php     
        for($x=0; $x < mysql_num_rows($result); $x++) {
            $out = ltrim($banner_id[$x], '@');
            echo "<li>$out</li>\n";
        }
    ?>