⑴ php如何用正則截取字元串
$content="這里是上面要正則的類容";
preg_match("/<Request>(.*)<\/Request>/iS",$content,$arr);
print_r($arr);
我測試過時成功的。數組保存了兩個元素,一個是帶有<Request></Request>的,另外一個是不帶有<Request></Request>的。
希望能幫到你!
⑵ php正則截取指定符號之間的字元串
按照你的要求截取第一個【和最後一個】之間的字元串的php程序如下
(用正則表達式中的貪婪匹配模式,取第一捕獲組的數據)
<?php
$str="擊鏈器【到手】得理多】";
$regex="/【(.*)】/";
preg_match_all($regex,$str,$result);
echo $result[1][0];
?>
⑶ php正則表達式字元串分割
在每個 .box 前面加個標示符,通過這個標示符分割
$source="<divclass='box'>
<divclass='title'>嗨,我是標題君1號</div>
<divclass='masll'>hi,我是副標題</div>
</div>
<divclass='box'>
<divclass='title'>嗨,我是標題君1號</div>
<divclass='masll'>hi,我是副標題</div>
</div>
<divclass='box'>
<divclass='title'>嗨,我是標題君1號</div>
<divclass='masll'>hi,我是副標題</div>
</div>";
$source=str_replace("<divclass='box'>","@@<divclass='box'>",$source);
$source=preg_replace('/^@@/','',$source);
$ary=explode('@@',$source);
print_r($ary);
⑷ php 正則表達式 截取字元串
<?php
$str
=
"<img
height=\"88\"
width=\"139\"
alt=\"\"
src=\"/do/admin/upload/news_image/kk0.jpg\"
/></p><p>
</p><p><img
style=\"width:
424px;
height:
205px\"
alt=\"\"
src=\"/do/admin/upload/news_image/kk3.bmp\"
/></p><img
height=\"88\"
width=\"139\"
alt=\"\"
src=\"/do/admin/upload/news_image/kk0.jpg\"
/></p><p>
</p><p><img
style=\"width:
424px;
height:
205px\"
alt=\"\"
src='/do/admin/upload/news_image/kk3.bmp'
/></p>";
$reg
=
'/\ssrc=[\'\"][\\/]([^\'"]*)[\'\"]\s/i';
preg_match_all($reg
,
$str
,
$out_ary);
//這個數組就是地址
$src_ary
=
$out_ary[1];
mp($out_ary);
mp($src_ary);
//格式化列印變數
function
mp($uStr){
echo
"<pre>";
var_mp($uStr);
echo
"</pre>";
}
?>
⑸ php正則截取字元串
<?php
$str=""=htt-----om/i7/361/060/36806784329/T1zQJMFxxaXXc"";
$reg="/"=(htt-----om.*)"/";
preg_match($reg,$str,$matches);
echo$matches[1];
?>
⑹ php 正則 分割 字元串
<?php
$str = '1,2,3,4,"5","a,d,v,x,g",5,s';
$preg1 = "/\"(.*?)\"/u";
$s = preg_replace_callback($preg1, 'callback', $str);
$s_arr = str_replace("~##~",",",explode(",",$s));
print_r($s_arr);
function callback($s) {
return str_replace(',', '~##~', $s[0]);
}
?>
- -!能勉強實現吧。。「這個雙引號要成對出現才行。。- -!坐等高手,我也學習一下。。
⑺ php正則表達式匹配分隔符
<?php
//php正則用任意分隔符(""",""/""!")分割字元串成數組
$str="Helloworld!Hello,world!Hello/world";
$str=preg_split("/[s,/!]+/",$str);
echo'<pre>';print_r($str);
?>
運行輸出為:
Array
(
[0] => Hello
[1] => world
[2] => Hello
[3] => world
[4] => Hello
[5] => world
)
希望對你有所幫助。
⑻ php分割字元串
/**
*寬字元串分割
*
*@paramunknown_type$str
*@returnunknown
*/
functionsplit($str){
$ret=array();
$len=strlen($str);
for($i=0;$i<$len;$i++){
//判斷編碼位數
$code=ord($str[$i]);
//單位元組0
if($code>>7==0){
$ret[]=$str[$i];
}
//4位元組1111
elseif($code>>4==15){
if($i<$len-3){
$ret[]=$str[$i].$str[$i+1].$str[$i+2]+$str[$i+3];
$i+=3;
}
}
//3位元組111
elseif($code>>5==7){
if($i<$len-2){
$ret[]=$str[$i].$str[$i+1].$str[$i+2];
$i+=2;
}
}
//雙位元組11
elseif($code>>6==3){
if($i<$len-1){
$ret[]=$str[$i].$str[$i+1];
$i+=1;
}
}
}
return$ret;
}
上面是分隔中文字串為數組的.
這種方式性能比正則表達式要高點. GBK , UTF-8 編碼都是支持的.
分隔完畢之後, 你在循環數組, 設置步長為3 . 然後就可以拼接出你要的了.
⑼ php如何分割中文字元串
php分割中文字元串,如果直接用PHP函數「str_split」來分割,會出現亂碼,因為中文字元長度和英文字元長度是不一樣的。
所以,可以建立新的函數先把字元轉成ASCII值,接著通過判斷不同字元的長度來正確分割中文字元串,把結果存入數組,最後再用PHP函數「join」在字元間插入百分號。
具體參考代碼如下:
<?php
functionstr_split_utf8($str){
$split=1;
$array=array();
for($i=0;$i<strlen($str)){
$value=ord($str[$i]);
if($value>127){
if($value>=192&&$value<=223)$split=2;
elseif($value>=224&&$value<=239)$split=3;
elseif($value>=240&&$value<=247)$split=4;
}else{
$split=1;
}
$key=NULL;
for($j=0;$j<$split;$j++,$i++){
$key.=$str[$i];
}
array_push($array,$key);
}
return$array;
}
$string="網路知道www..cn";
$arr1=str_split_utf8($string);
echojoin("%",$arr1);
?>
⑽ 探討PHP函數split()如何使用正則表達式切割字元串
如何使用PHP函數sprintf()將字元串格式化PHP函數preg_split的正確使用方法解讀PHP函數explode()的具體使用方法PHP函數implode()與explode()函數的不同PHP類CMS如何自動獲取關鍵字說明array split ( string $pattern, string $string [, int $limit] )提示preg_split() 函數使用了 Perl 兼容正則表達式語法,通常是比PHP函數split()更快的替代方案。