代码之家  ›  专栏  ›  技术社区  ›  Max

用php从数据中删除字符

php
  •  -1
  • Max  · 技术社区  · 6 年前

    在我用于此脚本的数据库表中,行包含字符(如“.”、“\u”)和数字。如何删除字符并只从表中获取数字? 我使用的php代码:

    <html>
    <head>
    </head>   
    <body>     
    
    <table class="tbnar">
    <tr>
    <th>Enex</th> 
    <th>Snex</th>
    </tr>
    
     <?php
    
      include ("config.php");
    
      $sql = "SELECT Enex, Snex FROM sntab";
      $result = $conn->query($sql);
      if ($result->num_rows > 0) {
    
      $counter = 0;
    
       while($row = $result->fetch_assoc()) 
       {
            echo  "</td><td>". $row["Enex"] . "</td><td>" . $row["Snex"].   
                "</td></tr>";
    
                $counter++;
                if($counter % 33 == 0) { ?>
    </table>
    
    <table class="tbnar">
    <tr>
    <th>Enex</th> 
    <th>Snex</th>
    </tr>
    
     <?php }
        }
    echo "</table>";
    
    } else { echo "0 results"; }
    $conn->close();
    ?>     
    </table>               
    
        </body>
    </html>
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   Felippe Duarte    6 年前

    你可以用正则表达式来做这个

    echo  "</td><td>". preg_replace('/[^0-9]+/','',$row["Enex"]) . "</td><td>" . preg_replace('/[^0-9]+/','',$row["Snex"]).   
            "</td></tr>";
    

    此正则表达式将删除所有非数字字符。