【php】基础学习1

  其中包括php基础、字符串和正则表达式的学习。具体如下:

  1 <html xmlns=http://www.w3.org/1999/xhtml>
  2     <head>
  3     <meta http-equiv=Content-Type content="text/html;charset=utf-8">
  4     </head>
  5     <body>
  6     <h1>
  7         <?php 
  8         /**
  9         * php输出语句:echo
 10         * 可以同时输出多个字符串,可以多个参数,并不需要圆括号,无返回值。
 11         * void echo ( string arg1 [, string ...] ) //返回值为空
 12         */
 13         echo "my first php demo!"; //echo输出
 14         echo "<br/>";
 15         echo 'my',' first',' php',' demo','!<br>';
 16         
 17         /**
 18         * php输出函数print
 19         * print() 和 echo() 用法一样,但是echo速度会比print快一点点。
 20         * 实际上它也不是一个函数,因此您无需对其使用括号。不过,如果您希望向print() 传递一个以上的参数,那么使用括号会发生解析错误。
 21         */
 22         print "hello<br>";
 23         print ("hello<br>");
 24         //print 'a','b','c'; ERROR这个是错误的
 25         ?>
 26     </h1>
 27     <h2>
 28         <?php
 29             $temp1="tem1";  //值赋值
 30             $temp2="tem2";
 31             $temp2=&$temp1; //引用赋值
 32             $temp1="woshi1";
 33             $temp2="woshi2";
 34             /**
 35             * php中的换行
 36             * ①\n只是源代码下的换行,而不是显示的效果,\n是在HTML源代码下可以看到换行;
 37             * ②PHP与C语言不同,PHP处理后给的对象是客户端的浏览器,而浏览器的换行是<br>标记;
 38             * ④\n是输出到文本文件时用到的换行,而要在浏览器中显示换行要用<br>或别的HTML标记;
 39             */
 40             echo $temp2."<br/>"; //php总换行可以用这种方式
 41             echo "huanghang\n";  //浏览器显示端并不换行
 42             
 43             /**
 44             * 超级全局变量:$GLOBALS,$_SERVER,$_SESSION,$_COOKIE,$_POST,$_GET,$_REQUEST
 45             */
 46             echo $GLOBALS."<br/>";
 47             echo $_SERVER."<br/>";
 48             echo $_COOKIE."<br/>";
 49             
 50             /**
 51             * 常量,php使用函数define来定义函数:boolean define(string name,mixed value[,bool case_insensitive]),返回值为1
 52             */
 53             $res=define("PI",3.141592);
 54             echo $res."<br>";
 55             $Pi2=PI*2;
 56             echo $Pi2."<br/>";
 57             printf("pi * 2 = %f",$Pi2);
 58             echo "<br>";
 59             
 60             /**
 61             * php中的双引号与单引号
 62             * ①PHP允许我们在双引号串中直接包含字串变量
 63             * ②单引号串和双引号串在PHP中的处理是不相同的。双引号串中的内容可以被解释而且替换,而单引号串中的内容总被认为是普通字符。
 64             */
 65             $color = "red";
 66             echo "Roses are $color"."<br>"; //双引号里的$color代表变量
 67             echo 'Roses are $color'."<br>"; //单引号里的$color代表字符串
 68             $s = "I am a 'single quote string' inside a double quote string"."<br>"; 
 69             echo $s;//双引号里嵌套单引号    
 70             $s1 = 'I am a double quote string" inside a single quote string'."<br>"; 
 71             echo $s1;//单引号里嵌套双引号
 72             //$s2 = "I am a "double quote string" inside a single quote string";——————ERROR双引号里不能嵌套双引号
 73             //$s2 = 'I am a 'double quote string' inside a single quote string';——————ERROR单引号里不能嵌套单引号
 74             $s3="I am a \"double quote string\" inside a single quote string"."<br>";
 75             echo $s3;//如果在这个串中想要表示出双引号,则可以使用转义符"\"(反斜线)
 76             $s3='I am a \'double quote string\' inside a single quote string'."<br>";
 77             echo $s3;//如果在这个串中想要表示出单引号,则可以使用转义符"\"(反斜线)
 78             
 79             /**
 80             * 字符串和整型相加时,字符串会转换为整型。
 81             */
 82             $a='5';
 83             echo $a+5;
 84             
 85             /**
 86             * 定界符
 87             * 是另一种给字符串定界的方法使用定界符语法(“<<<”)。应该在 <<< 之后提供一个标识符,然后是字符串,然后是同样的标识符结束字符串。
 88             * 注意:定界符的结尾一定要单独一行,而且要顶格写,前面不可以有空格。如下所示:
 89             
 90             $str="定界符的用法";
 91             echo <<<mark
 92                 用定界符输出字符串\$str=$str<br>\n
 93                 一个反斜线\\ &nbsp;&nbsp;&nbsp;&nbsp;一个双引号\"
 94 mark;
 95             //为了展示定界符的便利,下面我们输出一个包含了javascript的代码:
 96             $name = 'kitty';
 97             echo <<<Eof
 98                 <table height="20">
 99                 <tr>
100                 <td>
101                 {$name}<br/>
102                 <script>
103                     var p='hello world';
104                     document.writeln(p);
105                 </script>
106                 </td>
107                 </tr>
108                 </table>
109 Eof;
110             */
111             
112             /*
113             * php中substr
114             * 语法string substr ( string $string , int $start [, int $length ] )
115             * ①string为字符串,start为开始位置,length为截取长度;
116             * ②位置从0开始,当start为正数时从右向左截取;为负数时
117             */
118             $rest1 = substr("abcdef", 0, 0); // returns ""
119             $rest2 = substr("abcdef", 0, 2); // returns "ab"
120             $rest3 = substr("abcdef", 0, -1); // returns "abcde"
121             $rest4 = substr("abcdef", 2,0); // returns ""
122             $rest5 = substr("abcdef", 2,2); // returns "cd"
123             $rest6 = substr("abcdef", 2, -1); // returns "cde"
124             $rest7 = substr("abcdef", -2,0); // returns ""
125             $rest8 = substr("abcdef", -2,2); // returns "ef"
126             $rest9 = substr("abcdef", -2,-1); // returns "e"
127             echo $rest1,",",$rest2,",",$rest3,",",$rest4,",",$rest5,",",$rest6,",",$rest7,",",$rest8,",",$rest9,"<br>";
128             echo ">";
129             echo substr("hello shopNC",-7,-3)."<br>";
130             
131             /**
132             * php中的字符串拼接 "."
133             */
134             $string1="hello ";
135             $string2="world!";
136             echo $string1.$string2."<br>";
137             echo "{$string1}{$string2}"."<br>";//注意这里只能有双引号
138             $string1.=$string2;
139             echo $string1."<br>";
140             
141             /*
142             * printf()函数和sprintf()函数
143             * int printf(string $format[,mixed $arg1 [,mixed $arg2...])
144             * string sprintf(string $format[,mixed $arg1 [,mixed $arg2...])
145             * 其中format为制定要显示的字符串格式
146             * ①它们都可以对字符串执行格式化操作;
147             * ②printf可以将格式化后的字符串直接显示;
148             * ③sprintf则需要使用echo才可以将格式化后的字符串输出;
149             * %b二进制、%c以ASCII形式输出、%d整型,以有符号的形式输出
150             * %u整型,以无符号的形式输出、%o八进制、%x十六进制,字母部分为小写
151             * %X十六进制,字母部分为大写、%f浮点型、%s字符串
152             */
153             $string="%b,%c,%d,%u,%o,%x,%X,%f,%s";
154             printf($string,33,33,-33,33,-33,33,-33,33,-33);
155             echo "<br/>\n";
156             echo sprintf($string,33,33,-33,33,-33,-33,-33,33,-33)."<br>";
157             
158             /*
159             * nl2br()函数
160             * 该函数可以将字符串中的\n解释为HTML中的<br/>
161             */
162             $string="hello world1!\nhello world2!";
163             echo nl2br($string)."<br>";
164             
165             /*
166             * wordwrap()函数
167             * 可以让字符串在指定的位置强制换行,语法格式如下:
168             * string wordwrap(string $str [,int $width [, string $break [, bool $cut]]])
169             * str为要换行的字符串;width制定最大行宽度,默认为75;break制定作为分隔符的字符,默认为\n
170             * cut指定是否对大于指定宽度的单词进行换行,默认为false
171             */
172             $string=" welcometoChina!";
173             echo ">";
174             echo wordwrap($string,8,"<br>",true);
175             
176             /*
177             * 修改字母大小写
178             * string strtolower(string $str)所有字母转成小写
179             * string strtoupper(string $str)所有字母转成大写
180             * string ucwords(string $str)所有首字母转为大写
181             */
182             $string="welcome to China!";
183             echo "<br>".strtolower($string);
184             echo "<br>".strtoupper($string);
185             echo "<br>".ucwords($string);
186             
187             /*
188             * 计算字符串的长度strlen()函数
189             */
190             $string="welcome to China!";
191             echo "<br>".$string."的长度是:".strlen($string);
192             
193             /*
194             * substr_count()函数来计算制定的字符串在某个字符串中出现的次数,语法格式为:
195             * int substr_count(string $haystack,string $needle [, int $offset [, int $lengh]])
196             */
197             $string="welcome to China!welcome to Chinaaaa!";
198             $count=substr_count($string,"China");
199             echo "<br/>"."China在".$string."中出现的次数为:".$count;
200             
201             /*
202             * mixed str_word_count(string $string [, int $format [, string $charlist]])
203             * 返回字符串中单词的使用情况
204             * string为要检查的字符串,format指定返回模式,charlist规定被认定为单词的特殊字符
205             * 返回模式有三种:
206             * 0 默认值,返回找到的单词数目;
207             * 1 返回一个数组,其中,键名是以0开始的连续整数,值为实际单词
208             * 2 返回一个数组,其中,键名是单词在字符串中的首字母位置,值为实际单词
209             */
210             $string="welcome to China!! , welcome to Chinaaaa!!!";
211             $count=str_word_count($string);
212             echo "<br>"."字符串".$string."中共有".$count."个单词"."<br/>";
213             print_r(str_word_count($string,1));
214             echo "<br/>";
215             print_r(str_word_count($string,2));
216             echo "<br/>";
217             print_r(str_word_count($string,2,"!"));
218             echo "<br/>";
219             print_r(str_word_count($string,2,","));
220             
221             /*
222             * 字符串查找函数strstr()
223             * 该函数用来查找指定字符串在某字符串中的位置,该函数返回的结果为字符串第一次出现后的所有内容
224             * string strstr(string $str,mixed $needle,bool $before_needle);
225             * str为输入的字符串;needle要查找的字符串
226             * before_needel默认值为false;若为true,则返回needle在haystack中的位置之前的部分
227             * ①该函数区分大小写。如果想要不区分大小写,请使用 stristr()。
228             * ②如果你仅仅想确定 needle 是否存在于 str 中,请使用速度更快、耗费内存更少的 strpos()函数。
229             */
230             $email  = 'name@example.com';
231             $domain = strstr($email, '@');
232             echo "<br/>".$domain."<br/>"; // 打印 @example.com
233             $user = strstr($email, '@', true); // 从 PHP 5.3.0 起
234             echo $user."<br/>"; // 打印 name
235             
236             /*
237             * strpos()函数用来查找指定字符串在被检查的字符串中第一次出现的位置
238             * string strpos(string $haystack,string $needle,in [ offset])
239             * ①strpos()也有与之对应的对大小写不敏感的函数stripos()
240             */
241             $email  = 'name@example.com';
242             echo strpos($email,"@",2)."<br/>";//4
243             //上例中如果偏移量为5,则输出空
244             
245             /*
246             * str_repeat()函数
247             * string str_repeat ( string $input , int $multiplier )
248             * 返回input重复multiplier次数的结果
249             */
250             echo str_repeat("hello world!",3)."<br/>";//hello world!hello world!hello world!
251             
252             /*
253             * 字符串替换函数substr_replace()
254             * mixed substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] )
255             * string指定要检查的字符串;replacement指定要插入的字符串;start指定在字符串中开始替换的位置;
256             * length指定要替换的字符数,对length的几点解释:
257             * ①length如果不指定,默认长度为strlen($string);
258             * ②length为0,将replacement 插入到 string 的 start 位置处。
259             * ③length为正值,表示 string 中被替换的子字符串的长度
260             * ④length为负值,表示从字符串尾部开始到第|length|个字符停止替换
261             */
262             $string="hello world!";
263             echo substr_replace($string,"China",6)."<br/>";//hello China
264             echo substr_replace($string,"China",6,0)."<br/>";//hello Chinaworld!
265             echo substr_replace($string,"China",6,2)."<br/>";//hello Chinarld!
266             echo substr_replace($string,"China",6,-2)."<br/>";//hello Chinad!
267             
268             /*
269             * 字符串比较:
270             * ① "=="可以用来比较两个字符串是否相等;
271             * ② srcmp()用来比较两个字符串的大小(区分大小写)
272             * ② srcmp()用来比较两个字符串的大小(区分大小写)
273                 int strcmp(string $str1,string $str2);
274                 返回值为整数,返回负数则表示str1小于str2,返回正数则表示str1大于str2,返回0则表示两者相等
275                 该函数区别大小写
276             * ③ strcasecmp()二进制安全比较字符串(不区分大小写)
277                 strcasecmp()与strcmp()函数基本相同,只是strcasecamp()函数不区分大小写
278                 int strcasecmp ( string $str1 , string $str2 )
279             * ④ strnetcmp()是将字符串按自然排序法进行比较
280                 int strnatcmp ( string $str1 , string $str2 )
281                 该函数实现了以人类习惯对数字型字符串进行排序的比较算法,这就是“自然顺序”。注意该比较区分大小写。
282             */
283             $str1="hello aorld!";
284             $str2="hello China!";
285             echo strcmp($str1,$str2)."<br/>";//1
286             echo strcasecmp($str1,$str2)."<br/>";//-2
287             //strnetcmp()用法
288             $arr1 = $arr2 = array("img12.png", "img10.png", "img2.png", "img1.png");
289             echo "Standard string comparison\n";
290             usort($arr1, "strcmp");
291             print_r($arr1);
292             echo "<br>\nNatural order string comparison\n";
293             usort($arr2, "strnatcmp");
294             print_r($arr2);
295             
296             /*
297             * 正则表达式
298             * ?与{0,1}等价;*与{0,}等价;+与{1,}等价
299             */
300             
301             /*
302             * 使用正则表达式查找字符串 ,使用函数preg_match 
303             * 还有已经被废弃的两个函数是erge()和eregi(),它们完全相同只是eregi不区分大小写。
304             * int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
305             * ① pattern为要搜索的模式;subject为字符串;
306             * ② 如果提供了参数matches,它将被填充为搜索结果:$matches[0]将包含完整的模式匹配到的文本,matches[1]将包含第一个捕获子组匹配到的文本,以此类推;
307             * ③ flags可以被设置一下标记值:PREG_OFFSET_CAPTURE,如果传递了这个标记,对于每一个出现的匹配返回时会附加字符串偏移量(相对于目标字符串的)。
308             *      注意:这会改变填充到matches参数的数组,使其每个元素成为一个由第0个元素是匹配到的字符串,第一个元素是该匹配字符串在目标字符串subject中的偏移量。
309             * ④ 可选参数offset用于指定从目标字符串的某个位置开始搜索。 
310             */    
311             //1
312             $subject = "abcdef";
313             $pattern = '/^def/';
314             preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
315             echo "<br/>";
316             print_r($matches);//Array()
317             //2
318             $subject = "abcdef";
319             $pattern = '/^def/';
320             echo "<br/>".substr($subject,3)."<br/>";//Array()
321             preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE);
322             print_r($matches);//输出结果Array ( [0] => Array ( [0] => def [1] => 0 ) )
323             //检查电子邮件
324             $email="_.-~kifeandfork_@164.com";
325             echo "<br/>";
326             if(preg_match("/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$/",$email)){
327                 echo "your email's format is right!";
328             }else{
329                 echo "your email's format is wrong!";
330             }
331             
332             /*
333             * 字符串分割函数:正则表达式函数preg_split()
334             * array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
335             * pattern用于搜索的模式;subject输入的字符串;
336             * ① limit 如果指定,将限制分割得到的字串最多只有limit个,返回的最优一个字串将包含所有的剩余部分。
337                 limit的值为-1,0,null时都代表不限制。
338             * ② flags 可以去如下值:
339                 PREG_SPLIT_NO_EMPTY,此时函数将会返回分割后的非空部分;
340                 PREG_SPLIT_DELIM_CAPTURE,此时用分割的模式中的括号表达式将被捕获并返回;
341                 PREG_SPLIT_OFFSET_CAPTURE ,此时对于每一个出现的匹配返回时将会附加字符串偏移量
342             */
343             //1 使用逗号或空格(包含" ", \r, \t, \n, \f)分隔短语
344             $keywords = preg_split("/[\s,]+/", "hypertext language, programming");
345             echo "<br/>";
346             print_r($keywords);//输出Array ( [0] => hypertext [1] => language [2] => programming )
347             //2 将一个字符串分隔为组成它的字符
348             $str = 'string';
349             $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
350             echo "<br/>";
351             print_r($chars);
352             //3 分隔一个字符串并获取每部分的偏移量
353             $str = 'hypertext language programming';
354             $chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
355             echo "<br/>";
356             print_r($chars);
357             
358             /*
359             * 字符串分割函数:
360             * 如果你不需要正则表达式功能,可以有更快(并且更简单)的选择比如 explode() 或 str_split()。
361             * ① array explode ( string $delimiter , string $string [, int $limit ] )
362             *     delimiter为边界上的分割字符;string为输入的字符串;
363                 如果设置了limit参数并且是整数,则返回的数组包含最多limit个元素,而最后那个元素将包含string的剩余部分;
364                 如果limit为负数,则返回除了最后的-limit个元素外的所有元素
365                 如果limit为0,则会被当做1
366             * ② array str_split ( string $string [, int $split_length = 1 ] )
367             */
368             //1 函数explode()
369             $str = 'one|two|three|four';
370             echo "<br/>";
371             print_r(explode('|', $str, 2));//返回Array ( [0] => one [1] => two|three|four ) 
372             echo "<br/>";
373             print_r(explode('|', $str, -1));//返回Array ( [0] => one [1] => two [2] => three ) 
374             echo "<br/>";
375             print_r(explode('|', $str, 0));//Array ( [0] => one|two|three|four ) 
376             //2 函数str_split()
377             $str = "Hello Friend";    
378             $arr1 = str_split($str);
379             $arr2 = str_split($str, 3);
380             echo "<br/>";
381             print_r($arr1);
382             echo "<br/>";
383             print_r($arr2);
384 ?>
385     </h2>
386     </body>
387 </html>

 

posted @ 2014-03-22 16:15  NW_KNIFE  阅读(444)  评论(0编辑  收藏  举报