Translate

2013年9月24日 星期二

php 正規化 整理

1.只能輸入中文(不可數字.英文.特殊符號.空白鍵.全型符號等...)

     preg_match("/^\p{Han}$/", $address)
 2.只能輸入中文和數字(不可英文.特殊符號.空白鍵.全型符號等...)

    preg_match("/^\p{Han}*\d*$/", $address) //只能輸入中文

3.preg_match 與 preg_match_all差異
   preg_match        只會比對到第一個就結束了
   preg_match_all 會比對全部的值

4.(.+?) 與 (.+) 差異

preg_match('<div >(.+?)<\/div>'); 
preg_match('<div >(.+)<\/div>');  
這部份要自行體驗了

5.善用跳脫字元 讓表示模型更完整 "\"
   那那些符號需要被跳脫

+  /  \  *  ?  .  ^  $    [  ]   (  )  

6.htmlspecialchars 將html tag 轉成文字印出
  ex: echo "<td>
</td>";
      echo htmlspecialchars("<td></td>");



2013年9月18日 星期三

php 好用但不常用的function

addslashes($str);

ex 

<?php
$str 
"Is your name O'reilly?";// output: Is your name O\'reilly?echo addslashes($str);?>


simplexml_load_string($str)

<?php
$string 
= <<<XML<?xml version='1.0'?>
<document>
 <title>Forty What?</title>
 <from>Joe</from>
 <to>Jane</to>
 <body>
  I know that's the answer -- but what's the question?
 </body>
</document>
XML;
$xml simplexml_load_string($string);
print_r($xml);?>
 
$json = json_encode($xml);
$array = json_decode($json,TRUE);