本文目錄一覽:
- 1、PHP正則替換數字
- 2、php 正則替換變量數字 str_replace 怎麼用
- 3、php正則替換:2 9 8 8 12 15 + 8 這是一串數字(數字的個數不一定),數字間的空格數量不一定;
- 4、php如何進行正則替換
PHP正則替換數字
你這是一行N列的表格,可以用數組來表示td中的內容,然後用foreach遍曆數組用number_format函數格式化
php 正則替換變量數字 str_replace 怎麼用
$str=”news-123″;
$str=preg_replace(‘/news-(\d+)/’,’news’,$str);
echo $str;
換個函數哦,str_replace是不支持正則的
php正則替換:2 9 8 8 12 15 + 8 這是一串數字(數字的個數不一定),數字間的空格數量不一定;
我的方法比較麻煩參考一下吧
?php
$str=”2 9 8 8 12 15 8″;
$str_array=explode(” “,$str);//用空格分格數組
$str_array=array_filter($str_array);//過濾數組
$str_array=explode(“,”,implode(“,”,$str_array));//重新排序數組
$str_array_end=$str_array[count($str_array)-1];//獲取最後一個數組單元
array_pop($str_array);//將數組最後一個單元彈出(出棧)
$str_res=implode(“,”,$str_array);//用,連接數組單元
$result=$str_res.”+”.$str_array_end;//得到最終結果
echo $result;//結果:2,9,8,8,12,15+8
?
php如何進行正則替換
很簡單,代碼如下(其實不用正則也可以,strstr()與str_replace()函數也能替換):
// 需要替換的字符串
$string = ‘D:\wwwroot\cms\index.php’; // 假設一個路徑
// 正則樣式
$pattern = ‘/\\/’;
// 檢測是否需要替換
if (preg_match($pattern, $string)) {
// 開始替換\為/
$string = preg_replace($pattern, ‘/’, $string);
}
// 輸出替換後的字符串
echo $string; // D:/wwwroot/cms/index.php
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/285659.html