⑴ 幾個有用的php字元串過濾,轉換函數
explode — 使用一個字元串分割另一個字元串
array explode ( string $delimiter , string $string [, int $limit ] )
此函數返回由字元串組成的數組,每個專元素都是 string 的一個子屬串,它們被字元串 delimiter 作為邊界點分割出來。
<?php// 示例 1$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";$pieces = explode(" ", $pizza);echo $pieces[0]; // piece1echo $pieces[1]; // piece2 // 示例 2$data = "foo:*:1023:1000::/home/foo:/bin/sh";list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);echo $user; // fooecho $pass; // * ?>
⑵ php如何實現去掉某個字元串之後的內容
去掉某字元內容辦法
strstr(string,string) = strchr(,) //從前面第一次出現某個字元串的地方截取到最後
strrchr(string,string) //從某個字元串從最後出現的位置截取到結尾
strpos(string,string[,int]) //某個字元串第一次出現的位置
strrpos(string,string) //某個字元串最後一次出現的位置
substr(string,int[,int]) //從指定位置開始截取字元串,可以指定截取的長度。strlen(string) //獲取字元串的長度
⑶ php字元串過濾
^$str = preg_replace("/^.+=(.+)$/", "新內容\\1", $str); //過濾等於號前面的所有字元串(包括等於號)
$str = preg_replace("/^(.+)=.+$/", "\\1"."新內容", $str); //過濾等於號後面的所有字元串(包括等於號)
⑷ php 怎麼過濾後url後面的綴名
$_GET['id']='sdfsf.5.jpg';
$new_str= strrev($str);
$new_str1=substr($new_str,strpos($new_str, ".")+1);
echo strrev($new_str1)
⑸ PHP字元串過濾
可以用正則實現抄。給定字元串的規律是3冒號1分號中,前兩個冒號之前是數字,需要的是第三個冒號與分號之前的部分。可如下處理:
<?php
$string = "6939376:28407:包裝方式:包裝;5392114:75367881:重量(g):500;20000:7343430:品牌:蒙園;";
preg_match_all("/[^:\d]+:[^;]+;/", $string, $out);
$rst = implode("", $out[0]);
echo $rst;
?>
$rst為"包裝方式:包裝;重量(g):500;品牌:蒙園;"。已經測試是可行的(php5),如有問題歡迎繼續交流
⑹ php怎樣過濾掉特殊字元啊 ☺
過濾掉特殊字元,可以考慮使用字元串替換的方法,在php中替換字元效率最高也是最簡單字元替換函數str_replace函數。
使用方法:str_replace(find,replace,string,count)
參數說明:
find 必需。規定要查找的值。
replace 必需。規定替換 find 中的值的值。
string 必需。規定被搜索的字元串。
count 可選。一個變數,對替換數進行計數。
實例:
str_replace("iwind","kiki","iloveiwind,iwindsaid");
將輸出 "i love kiki, kiki said"
當然你也可以採取正則替換的方法,該函數是preg_replace
⑺ PHP字元串中特殊符號的過濾方法介紹
本篇文章主要是對PHP字元串中特殊符號的過濾方法進行了詳細的介紹,需要的朋友可以過來參考下,希望對大家有所幫助
有時候我們會遇到過濾字元串中特殊字元的問題,本文提供了一個處理特殊字元串的方法,可能有遺漏,如果讀者發現了可以
代碼如下:
function
strFilter($str){
$str
=
str_replace('`',
'',
$str);
$str
=
str_replace('·',
'',
$str);
$str
=
str_replace('~',
'',
$str);
$str
=
str_replace('!',
'',
$str);
$str
=
str_replace('!',
'',
$str);
$str
=
str_replace('@',
'',
$str);
$str
=
str_replace('#',
'',
$str);
$str
=
str_replace('$',
'',
$str);
$str
=
str_replace('¥',
'',
$str);
$str
=
str_replace('%',
'',
$str);
$str
=
str_replace('^',
'',
$str);
$str
=
str_replace('……',
'',
$str);
$str
=
str_replace('&',
'',
$str);
$str
=
str_replace('*',
'',
$str);
$str
=
str_replace('(',
'',
$str);
$str
=
str_replace(')',
'',
$str);
$str
=
str_replace('(',
'',
$str);
$str
=
str_replace(')',
'',
$str);
$str
=
str_replace('-',
'',
$str);
$str
=
str_replace('_',
'',
$str);
$str
=
str_replace('——',
'',
$str);
$str
=
str_replace('+',
'',
$str);
$str
=
str_replace('=',
'',
$str);
$str
=
str_replace('|',
'',
$str);
$str
=
str_replace('',
'',
$str);
$str
=
str_replace('[',
'',
$str);
$str
=
str_replace(']',
'',
$str);
$str
=
str_replace('【',
'',
$str);
$str
=
str_replace('】',
'',
$str);
$str
=
str_replace('{',
'',
$str);
$str
=
str_replace('}',
'',
$str);
$str
=
str_replace(';',
'',
$str);
$str
=
str_replace(';',
'',
$str);
$str
=
str_replace(':',
'',
$str);
$str
=
str_replace(':',
'',
$str);
$str
=
str_replace(''',
'',
$str);
$str
=
str_replace('"',
'',
$str);
$str
=
str_replace('“',
'',
$str);
$str
=
str_replace('”',
'',
$str);
$str
=
str_replace(',',
'',
$str);
$str
=
str_replace(',',
'',
$str);
$str
=
str_replace('<',
'',
$str);
$str
=
str_replace('>',
'',
$str);
$str
=
str_replace('《',
'',
$str);
$str
=
str_replace('》',
'',
$str);
$str
=
str_replace('.',
'',
$str);
$str
=
str_replace('。',
'',
$str);
$str
=
str_replace('/',
'',
$str);
$str
=
str_replace('、',
'',
$str);
$str
=
str_replace('?',
'',
$str);
$str
=
str_replace('?',
'',
$str);
return
trim($str);
}
⑻ php過濾字元串
用正則表達式替換。
因為你沒有詳細說明,所以我無法給你演示,找一本版正則表達式手冊,權如果你熟悉php,應該很容易看懂,在php裡面有正則表達式的相關函數,比如替換/匹配都很方便。
以下是幾個例子:
正則表達式說明
/\b([a-z]+) \1\b/gi一個單詞連續出現的位置
/(\w+):\/\/([^/:]+)(:\d*)?([^# ]*)/將一個URL解析為協議、域、埠及相對路徑
/^(?:Chapter|Section) [1-9][0-9]{0,1}$/定位章節的位置
/[-a-z]/A至z共26個字母再加一個-號。
/ter\b/可匹配chapter,而不能terminal
/\Bapt/可匹配chapter,而不能aptitude
/Windows(?=95 |98 |NT )/可匹配Windows95或Windows98或WindowsNT,當找到一個匹配後,從Windows後面開始進行下一次的檢索匹配。
⑼ php 去掉字元串中的一段字元串
可以用str_replace函數去掉字元串中的一段字元串。
1、新建php程序,定義一個變數,然後將一段字元串賦值給這個變數並將值輸出: