现在的位置: 首页 > 综合 > 正文

字符串的使用–串的分析

2013年05月21日 ⁄ 综合 ⁄ 共 717字 ⁄ 字号 评论关闭

一、串的分析

1.我想访问或改变字符串的一部分,而不是整个字符串

  substr()和substr_replace()函数。可读写字符串中的某一部分。

    $sub_str   =  substr($str , $initial_pos);

    $sub_str   =  substr($str , $initial_pos, $str_len);

    $new_str  =  substr_replace ($str , $replacement , $initial_pos);

    $new_str  =  substr_replace ($str , $replacement , $initial_pos , $str_len);

?>

strpos()函数可以给出指定的字符在字符串中第一次出现的位置:

   $email  =   'shenzhe163@sohu.com';

   $username = sub($email , 0 ,strop ($email , '@'));

?>

substr()  和 ereg()函数,可以匹配字符串中的一部分

if (ereg ("youpattern" , substr ($string ,-15))){

   print "The last 15 characters of string match the pattern";

}

?>
 

unpack()函数从字符串中提取二进制数据,并把它放入到一个相关数组中。

// read five characters and assign to $rec["zipcode"];

$rec  =  unpack ("A5zipcode" ,$str);

//Same thing whit substring

$rec ["xipcode"] = substr ($str , 0 , 5); 

?>

 

抱歉!评论已关闭.