久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      php怎么替換字符串中的所有字符

      3種替換方法:1、用substr_replace()從字符串頭部開始替換全部字符,語法“substr_replace(原字符串,指定替換值,0)”。2、用str_replace()替換全部字符,語法“str_replace(原字符串,指定替換值,原字符串)”。3、用str_ireplace()替換全部字符,語法“str_ireplace(原字符串, 指定替換值, 原字符串)”。

      php怎么替換字符串中的所有字符

      php入門到就業(yè)線上直播課:進入學(xué)習(xí)
      Apipost = Postman + Swagger + Mock + Jmeter 超好用的API調(diào)試工具:點擊使用

      本教程操作環(huán)境:windows7系統(tǒng)、PHP8.1版、DELL G3電腦

      方法1:利用substr_replace()函數(shù)

      substr_replace() 函數(shù)把字符串的一部分替換為另一個字符串。

      substr_replace(string,replacement,start,length)
      登錄后復(fù)制

      • substr_replace() 在字符串 string 的副本中將由 start 和可選的 length 參數(shù)限定的子字符串使用 replacement 進行替換。

      • 如果 start 為正數(shù),替換將從 string 的 start 位置開始。如果 start 為負(fù)數(shù),替換將從 string 的倒數(shù)第 start 個位置開始。

      • 如果設(shè)定了 length 參數(shù)并且為正數(shù),就表示 string 中被替換的子字符串的長度。如果設(shè)定為負(fù)數(shù),就表示待替換的子字符串結(jié)尾處距離 string 末端的字符個數(shù)。如果沒有提供此參數(shù),那么默認(rèn)為 strlen(string)(字符串的長度)。當(dāng)然,如果 length 為 0,那么這個函數(shù)的功能為將 replacement 插入 string 的 start 位置處。

      參數(shù) 描述
      string 必需。規(guī)定要檢查的字符串。
      replacement 必需。規(guī)定要插入的字符串。
      start 必需。規(guī)定在字符串的何處開始替換。

      • 正數(shù) – 在字符串的指定位置開始
      • 負(fù)數(shù) – 在從字符串結(jié)尾的指定位置開始
      • 0 – 在字符串中的第一個字符處開始
      length 可選。規(guī)定要替換多少個字符。默認(rèn)是與字符串長度相同。

      • 正數(shù) – 被替換的字符串長度
      • 負(fù)數(shù) – 從字符串末端開始的被替換字符數(shù)
      • 0 – 插入而非替換

      示例:替換字符串中的所有字符

      只需要將該函數(shù)的第三個參數(shù)設(shè)置為0,第三個參數(shù)設(shè)置為原字符串長度或省略即可替換全部字符

      <?php header('content-type:text/html;charset=utf-8');    $str = 'hello world!'; echo "原字符串:".$str."<br><br>"; $replace = 'ABCDEFGHIJKL'; echo "替換全部字符字符:".substr_replace($str, $replace,0)."<br>"; ?>
      登錄后復(fù)制

      php怎么替換字符串中的所有字符

      方法2/方法3:str_ireplace() 和 str_replace() 函數(shù)

      str_ireplace() 和 str_replace 都使用新的字符串替換原來字符串中指定的子字符串,如果需要替換的子串就是原字符串,則可替換原字符串中的所有字符。

      str_ireplace() 和 str_replace的語法是相似的,區(qū)別在于str_replace 區(qū)分大小寫,str_ireplace() 不區(qū)分大小寫

      str_replace(find,replace,string,count) str_ireplace(find,replace,string,count)
      登錄后復(fù)制

      參數(shù) 描述
      find 必需。規(guī)定要查找的值。
      replace 必需。規(guī)定替換 find 中的值的值。
      string 必需。規(guī)定被搜索的字符串。
      count 可選。對替換數(shù)進行計數(shù)的變量。

      示例:替換字符串中的所有字符

      只需要將第一個參數(shù)設(shè)置為原字符串值即可。

      <?php header('content-type:text/html;charset=utf-8');    $str = 'hello world!'; echo "原字符串:".$str."<br><br>"; $replace = 'ABCDEFGHIJKL'; echo "替換全部字符字符:".str_replace($str, $replace, $str)."<br>"; echo "替換全部字符字符:".str_ireplace($str, $replace, $str)."<br>"; ?>
      登錄后復(fù)制

      php怎么替換字符串中的所有字符

      擴展知識:替換字符串還可利用正則替換函數(shù)preg_replace() 和preg_filter()

      preg_replace() 和preg_filter()函數(shù)都可以執(zhí)行正則表達(dá)式的搜索和替換,不同的是 preg_filter() 函數(shù)只返回匹配成功的結(jié)果,而 preg_replace() 返回所有結(jié)果,不管是否匹配成功。

      preg_replace() 和preg_filter()函數(shù)的語法類似:

      preg_replace($pattern, $replacement, $subject [, $limit = -1 [, &$count]]) preg_filter($pattern, $replacement, $subject [, $limit = -1 [, &$count]])
      登錄后復(fù)制

      搜索 $subject 中匹配 $pattern 的部分, 以 $replacement 進行替換。

      參數(shù)說明如下:

      • $pattern:要搜索的模式,可以使一個字符串或字符串?dāng)?shù)組;

      • $replacement:用于替換的字符串或字符串?dāng)?shù)組。如果這個參數(shù)是一個字符串,并且 $pattern 是一個數(shù)組,那么所有的模式都使用這個字符串進行替換。如果 $pattern 和 $replacement 都是數(shù)組,每個 $pattern 使用 $replacement 中對應(yīng)的元素進行替換。如果 $replacement 中的元素比 $pattern 中的少,多出來的 $pattern 使用空字符串進行替換。

      • $subject:要進行搜索和替換的字符串或字符串?dāng)?shù)組,如果 $subject 是一個數(shù)組,搜索和替換回在 $subject 的每一個元素上進行, 并且返回值也會是一個數(shù)組。

      • $limit:可選參數(shù),每個模式在每個 $subject 上進行替換的最大次數(shù)。默認(rèn)是 -1(無限)。

      • $count:可選參數(shù),如果指定,將會被填充為完成的替換次數(shù)。

      示例:

      preg_filter()和preg_replace()利用正則來替換字符串

      <?php header('content-type:text/html;charset=utf-8'); $subject = array('1', 'a', '2', 'b', '3', 'A', 'B', '4');  $pattern = array('/d/', '/[a-z]/', '/[1a]/');  $replace = array('A:$0', 'B:$0', 'C:$0');    echo "preg_filter 返回值:n"; var_dump(preg_filter($pattern, $replace, $subject));    echo "preg_replace 返回值:n"; var_dump(preg_replace($pattern, $replace, $subject));  ?>
      登錄后復(fù)制

      php怎么替換字符串中的所有字符

      推薦學(xué)習(xí):《PHP視頻教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號