PHP----String

WBOY
Libérer: 2016-07-29 09:00:39
original
962 Les gens l'ont consulté

<code>var_dump(explode(<span>":"</span>,<span>"php:123"</span>));    <span>//返回array  按: 分割。 输出:php, 123</span><span>echo</span> implode(<span>','</span>, <span>array</span>(<span>'a'</span>,<span>'b'</span>));   <span>//返回string 输出:a b</span><span>list</span>(<span>$user</span>,<span>$pass</span>) = explode(<span>":"</span>,<span>"php:123"</span>);
<span>echo</span><span>$user</span>,<span>$pass</span>;      <span>//返回string 输出:php123</span><span>$array</span> = <span>array</span>(<span>'php'</span>,<span>'123'</span>);
<span>echo</span> join(<span>","</span>,<span>$array</span>); <span>//返回string 输出: php,123</span>strrchr(<span>'abab'</span>, <span>'a'</span>);           <span>//ab(最后一次出现)</span>
substr(<span>'abcd'</span>,<span>1</span>);               <span>//bcd(返回字符串的子串)</span>
substr(<span>'abcd'</span>,<span>2</span>,<span>2</span>);             <span>//cd</span>
substr(<span>'abcd'</span>,-<span>1</span>,<span>1</span>);            <span>//d(把位确认按长度截取)</span>
substr(<span>'abcd'</span>,-<span>2</span>,<span>2</span>);            <span>//cd</span>
strpos(<span>'abcd@yahoo.com'</span>, <span>'@'</span>);  <span>//4(定位字符)</span>
substr(<span>'abcd@yahoo.com'</span>, <span>0</span>,strpos(<span>'abcd@yahoo.com'</span>,<span>'@'</span>)); <span>//abcd</span>str_repeat(<span>'a'</span>,<span>5</span>);              <span>//aaaaa(重复一个字符串)</span>
str_shuffle(<span>'abcd'</span>);            <span>//cbda    注:随刷新变化(随机打乱一个字符串)</span>
stripos(<span>'Abca'</span>,<span>'a'</span>);            <span>//0 (注:a在第一个位子上,查找字符串首次出现的位置(不区分大小写))</span>
strripos(<span>'AbcA'</span>,<span>'a'</span>);           <span>//3 (计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写))</span>
strrpos(<span>'abcA'</span>,<span>'a'</span>);            <span>//0(计算指定字符串在目标字符串中最后一次出现的位置)</span>
substr_count(<span>'abcd'</span>,<span>'b'</span>);       <span>//1(计算字串出现的次数)</span>
strcspn(<span>'aabb cc'</span>,<span>'c'</span>);          <span>//5(返回在找到任何指定的字符之前,在字符串查找的字符数)</span>
strspn(<span>'abcdef'</span>,<span>'abc'</span>);          <span>//3(返回在字符串中包含的特定字符的数目)</span>
strspn(<span>'abcdef'</span>,<span>'bc'</span>);           <span>//0</span>
strspn(<span>'abcdef'</span>,<span>'bc'</span>,<span>1</span>,<span>5</span>);       <span>//2</span>chr(<span>65</span>);                        <span>//A (获取ASCII编码)</span>
ord(<span>'A'</span>);                       <span>//65</span>
strchr(<span>'Aa bb'</span>,<span>'a'</span>);            <span>//a bb(搜索一个字符串返回指定的字符)</span>
strchr(<span>'Aa bb'</span>,<span>99</span>);             <span>//为空       注:ASCII  c:99</span>
strchr(<span>'Aa bb'</span>,<span>97</span>);             <span>//a bb       注:ASCII  a:97  </span>
addcslashes(<span>'aabb'</span>,<span>'a'</span>);        <span>//\a\abb(函数在指定的字符前添加反斜杠)</span>
addslashes(<span>"aa bb'"</span>);           <span>//aa bb\'(转义:预定义',\)</span>lcfirst(<span>'Hello World'</span>);           <span>//hello World(第一个字符小写)</span>
ltrim(<span>":php:123:"</span>,<span>':'</span>);           <span>//php:123(去掉左右字符)</span>
rtrim(<span>":php:123:"</span>,<span>':'</span>);           <span>//:php:123(去掉右字符)</span>
trim(<span>":php:123:"</span>,<span>':'</span>);            <span>//php:123(去掉左字符)</span>nl2br(<span>"php\r\n123"</span>);                        <span>//php<br>  123(插入转行标签)</span>
wordwrap(<span>'aaabbb'</span>,<span>3</span>,<span>'<br>'</span>, <span>true</span>);          <span>//aaa<br>bbb(打断字符串为指定数量的字串)</span>
quotemeta(<span>'abcd.()'</span>);                       <span>//abcd\.\(\)---(引用元字符:. + * ? [ ^ ] ( $ ) )</span>
similar_text(<span>'abcd'</span>,<span>'aaaa'</span>,<span>$sim</span>);           <span>//25(计算两个字符串的相似度)</span>
similar_text(<span>'abcd'</span>,<span>'aaaa'</span>);                <span>//1</span>
sprintf(<span>'name:%s,age:%d'</span>,<span>'aaa'</span>,<span>12</span>);         <span>//(返回格式化字符串)</span>str_ireplace(<span>'aa'</span>,<span>'11'</span>,<span>'AA22'</span>);             <span>//1122(忽略大小写版本)</span>
strcasecmp(<span>'Hello'</span>,<span>'hello'</span>);                <span>//0   注:left>right:正,反之:负(二进制安全比较字符串(不区分大小写))   </span>
str_pad(<span>'aaa'</span>,<span>10</span>);                          <span>//aaa       (后跟7个空格)--(使用另一个字符串填充字符串为指定长度)</span>
str_pad(<span>'aaa'</span>,<span>10</span>,<span>'-'</span>,STR_PAD_LEFT);         <span>//-------aaa;</span>
str_pad(<span>'aaa'</span>,<span>10</span>,<span>'-'</span>,STR_PAD_BOTH);         <span>//---aaa----</span><span>$str</span> = <span>"<a href="#">hello<p>1</p></a>"</span>;
strip_tags(<span>$str</span>);             <span>//hello1     (从字符串中去除 HTML 和 PHP 标记)</span>
strip_tags(<span>$str</span>,<span>'<p>'</p></span>);       <span>//hello<p>1</p></span>
stripcslashes(<span>'aa\\bb'</span>);      <span>//aab(转义的字符串 去掉反斜杠)</span>
stripcslashes(<span>'aa\\\bb'</span>);     <span>//aa\bb</span>stripslashes(<span>'This is \0 one'</span>);      <span>// This is one(反引用一个引用字符串)    </span>
stristr(<span>'ABc@163.com'</span>,<span>'b'</span>);          <span>//Bc@163.com(函数的忽略大小写版本)</span>
stristr(<span>'ABc@163.com'</span>,<span>'b'</span>,<span>true</span>);     <span>//A</span>
strstr(<span>'ABc@163.com'</span>,<span>'@'</span>);           <span>//@163.com(查找字符串的首次出现)</span>
strstr(<span>'ABc@163.com'</span>,<span>'@'</span>,<span>true</span>);      <span>//ABc</span>strlen(<span>'ab cd'</span>);                <span>//5(获取字符串长度)</span>
strcmp(<span>"aaa"</span>, <span>"bbb"</span>);           <span>//-1       注: 二进制安全字符串比较</span>
strncmp(<span>'abcd'</span>,<span>'ab'</span>,<span>2</span>);         <span>//0        注:2个匹配0,部分1,无-1</span>
strncmp(<span>'abcd'</span>,<span>'a'</span>,<span>2</span>);          <span>//1</span>
strncmp(<span>'abcd'</span>,<span>'c'</span>,<span>2</span>);          <span>//-1</span>substr_compare(<span>'abcd'</span>,<span>'b'</span>,<span>1</span>,<span>1</span>);         <span>//0(二进制安全比较字符串(从偏移位置比较指定长度))</span>
substr_compare(<span>'abcd'</span>,<span>'c'</span>,-<span>2</span>,<span>1</span>);        <span>//0</span>
substr_compare(<span>'abcd'</span>,<span>'B'</span>,<span>1</span>,<span>1</span>,<span>true</span>);    <span>//0</span>
substr_replace(<span>'abcd e'</span>,<span>'1'</span>, <span>5</span>);         <span>//abcd 1(替换字符串的子串)</span>
substr_replace(<span>'abcd e'</span>,<span>'1'</span>, -<span>1</span>);        <span>//abcd 1</span>
substr_replace(<span>'abcd e'</span>,<span>'1'</span>, <span>2</span>,<span>2</span>);       <span>//ab1 e</span>str_replace(<span>'aa'</span>,<span>11</span>,<span>'aa22'</span>);        <span>//1122(子字符串替换)</span>
strpbrk(<span>'abAc'</span>,<span>'A'</span>);                <span>//Ac(在字符串中查找一组字符的任何一个字符)</span>
strpbrk(<span>'abAc'</span>,<span>'cb'</span>);               <span>//bAc(在字符串中查找一组字符的任何一个字符)</span>
strrev(<span>'abcd'</span>);                     <span>//dcba(反转后的字符串)</span>strtok(<span>'aa/bb'</span>,<span>'/'</span>);       <span>//aa(标记分割字符串)</span>
strtolower(<span>'ABc'</span>);         <span>//abc(将字符串转化为小写)</span>
strtoupper(<span>'ABc'</span>);         <span>//ABC(将字符串转化为大写)</span>
ucfirst(<span>'abcd'</span>);           <span>//Abcd(将字符串的首字母转换为大写)</span>
ucwords(<span>'abcd cc'</span>);         <span>//Abcd Cc(将字符串中每个单词的首字母转换为大写)</span>trtr(<span>'aabbcc'</span>,<span>'ac'</span>,<span>'11'</span>);               <span>//11bb11(去除字符串首尾处的空白字符)</span><span>$arr</span> = <span>array</span>(<span>'hello'</span> => <span>'hi'</span>, <span>'world'</span> => <span>'today'</span>);
<span>echo</span> strtr(<span>'hello world'</span>,<span>$arr</span>);            <span>//hi today</span>trim(<span>' abcd '</span>);                 <span>//abcd(去除字符串首尾处的空白字符)</span>
trim(<span>'xabcdx'</span>,<span>'x'</span>);             <span>//abcd</span>md5_file(<span>"C:\d\d.txt"</span>);         <span>//d.txt 文件要存在</span>
md5(<span>"C:\d\d.txt"</span>);

<span>//双重md5加密</span><span><span>class</span><span>TestMd5</span>{</span><span><span>function</span><span>get</span><span>(<span>$num</span>)</span>{</span><span>$num</span>=md5(md5(<span>$num</span>).<span>"En"</span>);
        <span>return</span><span>$num</span>;
    }
}

<span>$testMd5</span> = <span>new</span> TestMd5();
<span>echo</span><span>$testMd5</span>->get(<span>"test"</span>);

<span>$date</span> = date(<span>"Y-m-d H:i:s"</span>);
<span>list</span>(<span>$y</span>,<span>$m</span>,<span>$d</span>,<span>$h</span>,<span>$i</span>,<span>$s</span>) = split(<span>'[- :]'</span>, <span>$date</span>);    
<span>echo</span><span>"$y 年$m 月 $d 日 $h 时 $i 分$s 秒"</span>;  <span>//分解字符串</span>number_format(<span>1234.56</span>);                     <span>//1,2345(格式化数字)</span>
number_format(<span>1234.56</span>,<span>2</span>,<span>','</span>,<span>''</span>);            <span>//1234,56</span>
number_format(<span>1234.56</span>,<span>2</span>,<span>'.'</span>,<span>' '</span>);           <span>//1 234.56</span><span>$str</span> = <span>'first=one&arr[]=a+b&arr[]=ccc'</span>;
parse_str(<span>$str</span>);           <span>//解析字符串变量</span><span>$first</span>;                     <span>//one</span><span>$arr</span>[<span>0</span>];                    <span>//a b</span><span>$arr</span>[<span>1</span>];                    <span>//ccc;</span>parse_str(<span>$str</span>,<span>$put</span>);
<span>$put</span>[<span>'first'</span>];             <span>//one</span><span>$put</span>[<span>'arr'</span>][<span>0</span>];            <span>//a b</span><span>$put</span>[<span>'arr'</span>][<span>1</span>];            <span>//ccc</span><span>$arr1</span> = <span>array</span>(<span>'img1.png'</span>,<span>'img10.png'</span>,<span>'img2.png'</span>);
sort(<span>$arr1</span>);               <span>//按key值</span>
natsort(<span>$arr1</span>);                <span>//按val值</span><span>$line</span> = <span>'aa "bb cc." dd.txt'</span>; 
<span>$parsed</span> = str_getcsv( <span>$line</span>,<span>' '</span>,<span>'"'</span>);        <span>//解析 CSV 字符串为一个数组</span>
var_dump( <span>$parsed</span> );
<span>//array(3) { [0]=> string(2) "aa" [1]=> string(6) "bb cc." [2]=> string(6) "dd.txt" }</span>print_r(str_split(<span>'hello world'</span>));  <span>//将字符串转换为数组</span>
print_r(str_split(<span>'hello world'</span>,<span>3</span>));
<span>/*
Array
(
    [0] => h
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>  
    [6] => w
    [7] => o
    [8] => r
    [9] => l
    [10] => d
)
Array
(
    [0] => hel
    [1] => lo 
    [2] => wor
    [3] => ld
)
*/</span></code>
Copier après la connexion
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了PHP----String,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal