从字符串的开头删除所有出现的字符串

时间:2017-10-09 11:49:14

标签: php

我需要在字符串的开头删除所有出现的“0”:

00000012322   //  i would like to get 12322
0011330322    //  i would like to get 11330322

我怎么能用PHP做到这一点?

2 个答案:

答案 0 :(得分:3)

使用PHP的ltrim()功能:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>

<div class="container">
  <div class="form-group">
  <div class="row">
    <div class="col-3">
     <label for="exampleFormControlInput1">Bobs</label>
    </div>
    <div class="col-6">
    <textarea class="form-control" id="exampleFormControlTextarea1" rows="3"></textarea>
    </div>
    <div class="col-3">
    I need text here
    </div>
  </div>
</div></div>

答案 1 :(得分:1)

您可以通过强制PHP将其更改为数字

来轻松破解它
$var = "00000012322";
echo $var+0;
//or
echo floatval($var);

我想注意,这会导致您的变量类型改变,这可能不是您想要的!正如其他人指出的那样,ltrim()可以做到这一点。您也可以使用strval()转换回字符串,但这肯定比ltrim更昂贵......所有都取决于用法。