Monday, May 9, 2011

Client-side HTML table pagination with JavaScript - NewInstance

Client-side HTML table pagination with JavaScript - NewInstance

Most of the times, you don't need to paginate on the client side: if you have an enough small set of records to be displayed, I would suggest you to choose a scrollable
.
Server-side web pagination is really needed when you have to display hundreds of records. You may fetch results from the DB using an offset and loading a single page for each HTTP request.
If your resultset is small it's possible to load it fully. Sometime I saw code loading the full resultset in session; and on each page-switch the browser refreshes displaying a different subset/page. There are many disadvantages of a server-side pagination like this:
  • You have to use the HTTP Session (and remember to clean up when it's nomore needed).
  • You have to reload the page on every page switch.
  • You cannot go forward and backward to check previous data without loosing the view on current resultset.
  • You have to write code for the server side actions handling all the page-switch work.
As I said before, everything would be solved using a table inside scrollable div but, in my current web application "GUI designers" like to persecute the users having them clicking and clicking even for few records. As in my case, I have an "editable table" (an html form) that's also paginated, in wich validation of each record depends on other records...
At least I wanted to avoid reloading the page on every page switch, and also avoid using HTTP Session when not strictly necessary. So I end up writing a JavaScript paginator object that handles the job of paginating any html table rendering also a simple pagination bar.
Here's a live sample:
# field
1
2
3



« Prev | 1 | 2 | 3 | 4 | Next »

That's all. If you want to download a demo (html+javascript files), you can click here: JavaScript Table Paginator Demo.
If you have a huge table, maybe it will be displayed fully before the javascript at the bottom will be executed. To avoid this, you can set the table as hidden by default using css style, and make it visibile using JavaScript after the pager.showPage() call.

Monday, May 2, 2011

How to rename a file uploaded with Zend_Form_Element_File

How to rename a file uploaded with Zend_Form_Element_File

There are several ways of renaming files if you don’t want to use Zend_Form_Element component. However, the following is a ZF technique.Suppose you have a very basic form with a single file element:

Form.php
class My_Form extends Zend_Form { 
public function init()     { 
$element = new Zend_Form_Element_File('imagefile'); 
$element->setLabel('Upload an image:'); 
$element->setDestination($uploaddir); 
/* file directory */ 
$element->addValidator('Count', false, 1);    
$element->addValidator('Size', false, 202400);    
$element->setRequired(false); 
$element->addValidator('IsImage', false, 'jpeg,jpg,png,gif'); 
$this->addElement($element, 'imagefile'); 
} 
}
 
What you need to do is to override the isValid( .. ) method of your Form Object:

Form.php
public function isValid($data) { 
$oldname = pathinfo($this->imagefile->getFileName());    
$newname = $this->getUniqueCode(10) . '.' . $oldname['extension'];    
$this->imagefile->addFilter('Rename',$newname);    
return parent::isValid($data);      
}
 
If you don’t want to loose your other validators , don’t forget to call the parent isValid() method . As you can see, I used PHP’s pathinfo() method to parse the file path. (There should be another one in ZF ). I renamed it with a string of 10 random characters, and kept the extension as it was. Below is the method that you can use for the random file name :

function getUniqueCode($length = "") {     
$code = md5(uniqid(rand(), true));    
if ($length != "")  return substr($code, 0, $length); 
else   return $code; 
}
Finally, in your controller, you basically check if the form is valid,

MyFormController.php
$request = $this->getRequest();  
$form  = new My_Form(); 
if ($request->isPost()) { 
if ($form->isValid($request->getPost())) { 
$formData = $form->getValues(); 
try {      $form->imagefile->receive();    } 
catch (Exception $e) {     throw new Zend_Exception ('There is a problem with the file upload');        } 
}  }
 
I hope it helps.

Tuesday, April 19, 2011

Multiple Doctrine relations on the same table

From: http://www.odino.org/231/multiple-doctrine-relations-on-the-same-table


Using symfony with Doctrine if you have a model which is related to another mdoel you're used to write stuff in the schema like this:

Model_1:
  columns:
    model_2_id:  ...
  relations:
    Model_2:
      local:     model_2_id
      foreign:  id

which works perfectly unless you specify that another column is related to Model_2 in the same way of the first:

Model_1:
  columns:
    model_2_id:         ...
    model_2_second_id:  ...
  relations:
    Model_2:
      local:     model_2_id
      foreign:  id
      alias:     FirstModel2
    Model_2:
      local:     model_2_second_id
      foreign:  id
      alias:     SecondModel2 

This messes things up because, as sfYaml converts YAML to plain PHP array, 2 elements have the same key ( Model_2 ) and the resulting SQL will create one relation instead of 2.
The solution is simple ( this is a silly error, I often forget it due to the lack of attention ), you only need to extract the alias and specify the class for each relation:

Model_1:
  columns:
    model_2_id:         ...
    model_2_second_id:  ...
  relations:
    FirstModel2:
      class:     Model2
      local:     model_2_id
      foreign:  id
    SecondModel2:
      class:     Model2
      local:     model_2_second_id
      foreign:  id

I also noticed that you can magically use a single relation ( but without the possibility, I suppose, to specify custom aliases [ not sure about this ] ):

Model_1:
  columns:
    model_2_id:         ...
    model_2_second_id:  ...
  relations:
    Model2:
      local:     [model_2_id, model_2_second_id]
    foreign:  id

This one is normally pretty useless ;-)

Wednesday, April 13, 2011

坐月子是女性唯一可以改变体质的机会【男同胞们,别害羞,一定要保存好...】

坐月子是女人一生中最好的改善体质时机。如果没有坐好月子,将为以后的身体健康埋下隐患。中国人讲究坐月子,千万不要以为那是迷信哦,许多讲究都是有道理 的。 

一、生活上要注意: 

1.一定要休养一个月或一个半月。产后两周,除了吃饭和上洗手间以外,其他的时间一定要卧 床。若常坐或起身走动,可能引起子宫下垂。 

2.准备两三条宽度约为30---40厘米,长度可绕腹部12圈半的白纱布。产后要紧绑腹 带,防止内脏下垂。内脏下垂将导致妇女病,以及小腹突出,体形难看。绑腹带还可改善产前小腹明显的状况。腹带不能以一般的束裤和束腹带代替。因为它们不但 没有效果,还有反作用。 

3.前两周洗澡只能用温酒水擦澡。做法是烧开的水和米酒各一半,加一点点盐巴,用毛巾浸湿,拧干,在身上擦。第 三周起可以淋浴,满月后可泡澡。(顺产者) 

4.洗脸刷牙须用烧开过的水放至温热再使用。可以使用保养品。 

5.严禁洗 头。因为头皮不能着凉。 

6.不能抱小孩。否则可能内脏下垂。喂奶时可侧躺着喂。 

7.不能为小孩洗澡,否则可导致腰酸 背痛及手脚酸麻。所以,给小孩洗澡的时要交给LG等人。 

8.要有安静,舒适的环境。阳光太强时要拉上窗帘,保护眼睛。不能吹风,因为产 后全身毛孔都张开了,吹风易引起头风及关节酸痛等,连扇子也不要摇。 

9.月子期间不能爬楼梯,产后半年内不能提重物,避免内脏下垂。 

10. 不要流泪,否则眼睛将提早老化,可能演变为青光眼或白内障。少看书报和电视,一定要让眼睛充分休息。 

二、饮食方面要讲究: 

1. 少食多餐。 

2.产后半个月内严禁喝水、饮料及汤类,牛奶也不可以喝。因为会喝成大水桶,会变胖,将来容易得风湿病或神经痛。可以用烧开 的米酒代替水分(买商店里的瓶装米酒)。做法是将数瓶米酒倒入锅内,不加盖,大火煮沸后持续15分钟以上。再将煮好的米酒水装入热水瓶备用。 

3. 不要吃盐,生冷的食物,酸的东西也不能吃。(有理由的,很长我就不说了。) 

4.半个月内避免吃青菜和水果。可吃点红萝卜。月子内要吃水 分少的水果。 

5.半个月内禁吃鱼类和肉类,可以多吃肝脏等。因为疲劳的身体无法完全吸收鱼、肉的养分,结果过多的养分,只会加重身体的 负担,产生“虚不受补”的现象。应该到第三周开始吃。 

6.用麻油、姜和米酒料理食物。姜要爆透。

小S坐月子的方法,难 怪她身材那么好!(怕弄丢,还是转载比较好)

本计划书根据台湾庄淑旗博士的坐月子理论,本人将逐一实践,并及时把心得体会与各位姐妹分 享。 
关键词语: 
1、 滴水不沾 (以米酒水代替普通水)
2、 粒盐不进 (少食盐)
3、 勤绑腹带 
4、 静心休养 

二、《如何坐月子》理论概要

初次知道这本书,是因为一个朋友丽丽。那时 候我姐姐怀孕快生了,丽丽介绍她用这本书,说效果不错。我才知道她的小孩已经半岁了,可是她的身材恢复得非常好,根本看不出生过小孩了。就这样,我们家姐 妹三个,都用这个方法坐的月子,不仅身材恢复的不错,体质也比以前好多了。 
作者庄淑芹博士,是台湾著名的妇产科专家。她提出,女性在一生中有三次机会可以调整自己的体型,使之恢复青春、美丽与健康,那就是初潮期、生产后、更年 期。这里最重要的就是生产后这个阶段。这个时期,你的身体就好像一个打开的大门,可以把怀孕时积累的多余的积水和毒素(这也是产后发胖的主要原因)排出体 外,然后通过正确的补养使你的身体更健康。 

1、坐月子三阶段:一排二调三补
具体是将产后一个月按周划分,每周根据 需要,吃不同的食品: 
第一周,主要是要把多余的水分和毒素以及恶露排出体外,每天喝生化汤,吃麻油炒猪肝。注意不能喝水(否则排水的效果就 没了),渴了就喝止渴饮料,如荔枝核、山楂片等,或煮开的米酒。任何菜都不能放水,用米酒煮开代替。不能放盐(盐会使水分停留在身体里),不能放醋(醋会 使骨质变软),不能放酱油(道理同盐)。 

第二周,主要增强骨质和腰肾的功能,恢复骨盆。每天吃炒腰子和杜仲粉,有助于缓解尾椎 骨等骨疼;(另:如果是剖腹产,还要再喝一周生化汤)。禁忌同上,也不要吃咸菜,泡菜,味曾汤。 

第三周到一个月,这时候,该排 的已经排完,主要吃麻油鸡补身体。千万记得,排完毒素后吃麻油鸡才能被身体吸收,否则堆积在身体里,成为多余的脂肪。 因为前期身体虚弱,中医讲虚不受补,吃了吸收不了,只会加重身体的负担。

以上所有的食物,都要用到大量的米酒(不能用料酒和黄 酒,里面含盐且工艺不同),生姜,胡麻油,红糖。

2、 同时,坐月子期间,不能吹风,着凉。要穿有后帮的鞋(不能穿拖鞋,否则落下脚后跟疼的毛病)。洗脸用煮过的米酒放温后洗,不要洗澡,同样用煮过的米酒擦 身。绑腹带,最好自己喂奶,对身体的恢复会更好。出了月子之后再开始运动,等等,不一而足。 
我自己生小孩前110斤,生的时候135斤。小孩生下来8斤5两,生完4天时,113斤,出月子时就恢复到110斤。但是这时的体形很松跨,没有肌肉,肚 子松松的。不过自己带小孩很累,加上偶尔的锻炼(抽时间锻炼也不容易啊),6个月后,体重已经变成98磅,体形也慢慢恢复好了,别人都看不出我生过小孩。 没有胯变宽之类的后遗症,但是肚子上的妊辰纹消不下去了。 
快生的时候腿已经有了浮肿,手关节也有些疼和肿大,但是坐完月子后,基本上就好
了。生完后,尾椎骨疼,在第二个礼拜吃完杜仲粉后,也好了。 
有人觉得不吃盐太难了,其实我自己平时吃饭口味就淡,月子期间不吃盐也没觉得怎
样。想想为了以后的健康和身材,一个月不吃盐又算什么呢?总比 减肥舒服吧。
三、坐月子系列物品 

1、米酒水 
在北京艾佳人月子公司的资料介绍中,有一个米酒水随处都可以 见到,到处都要用到。这个水他们卖得比较的便宜,要240元/箱,30天月子坐下来,要10箱,也就是说要2400元。但是根据资料的介绍,这个水实际上 就是米酒烧开,不要加盖,让酒精充分地挥发。整个月子期间,要用这个水来代替产妇一切的饮食用水,包括洗菜、泡米,炒菜、煮饭、擦澡等等等等。 
这里特别说明,这个米酒不是国内说的醪糟或酒酿,是专门的月子米酒,酒精含量在15度的样子。 
2、 生化汤 
生化汤是一付配 好的中药,它的主要功能是使子宫尽快排出恶露,帮助它恢复正常的位置和形状。稍微大一点的中药房,都可以买到。 
3、胡麻油 (黑麻油)
胡麻油,这个在月子期间几乎所有的菜里都会用到。 有防止皮肤衰老和抗癌的作用
4、腹带 
长1500CM,宽15CM的白纱 带,需备至少两条。艾佳人两条卖100元,我计划去药店购买,如果没有,用绷带在缝纫机上按规格自制。 
5、杜仲粉。产后第二周服用。
四、 各种餐点的制作方法 

1、 产妇的饮料: 
材料:山楂肉、荔枝壳、观音串加入十倍米酒水中,烧开烧滚,滤出。加黑砂 糖搅拌均匀,在容器中冷藏。喝时加热,每天不超过500CC,可一次多做点。山楂肉、荔枝壳、观音串可以单独选用,也可以搭配使用。 
2、生 化汤: 
材料:当归,川穹,桃仁(去芯),烤老姜,炙草(蜜甘草)。 
做法: A米酒700cc,加入药料,慢火煮1小时左右,约剩200cc,这是第一次,药酒倒出备用。 
B第二次再加入米酒350cc,和第一次煮法 相同,约剩100cc。 
吃法: 把A和B 放在一起,一天分3次喝完。顺产喝7天,剖腹产喝14天。 
3、养肝汤: 
如果是剖腹产,要在产前一周吃养肝汤,产后接着吃两周(产后用米酒煮)。 
材料: 鸡心枣(要买红的鸡心枣)产前用热水(产后用米酒水)280cc 
做法: 用水洗净红枣,以刀切开7处。 放在容器中,将热开水冲下,加盖放置一夜。 第二天早上用蒸器蒸之。 等沸腾后再用文火蒸一小时。 
吃法: 把红枣取出,去皮去种子,一天之中随时可食。汤汁可分两三次,饭后当茶喝。 加点冰糖或白兰地亦可。 
4、薏仁饭: 薏仁加白米煮成饭,用米酒水煮。 
5、麻油炒猪肝的方法(猪腰做法相同) 
材料:猪肝用米酒洗净,切成1公分厚度,体重每10公 斤要取60克老姜(连皮一起切片)体重每十公斤取六克。 麻油,体重每十公斤取6cc. 米酒,体重每十公斤用60cc. 
做法: 老姜用麻油炒香,成浅褐色,姜捞起,先置于备好的米酒中。油热,大火,再放下猪肝,用大火快炒,再倒入浸着姜的米酒煮开。 
吃法: 猪肝适宜于早、中餐吃,以上分量可以分3——4次吃完; 
注:新鲜猪腰,每天一副,用米酒擦干后切成两半,剔除白色尿腺。将清理干净的腰子在 表面斜切裂口后,再切成3公分的小片。 
6、甜糯米粥 
材料(三日份):糯米,桂圆肉,黑糖,米酒水2000CC。 
做法:将糯米与桂圆肉放入米酒水中,加盖泡8小时。 将泡过的材料,以大火煮滚后改以小火加盖煮1小时。 熄火,加入黑糖搅拌后食用。 
7、红豆汤: 
材料(三日份):红豆,带皮老姜、黑糖,米酒水1500CC。 
做法:将红豆放入米酒水中,加盖泡8小时。老姜切 成丝,放入已泡好的红豆中。大火煮滚后转中火继续煮20分钟(加盖)。熄火,加入黑糖搅拌后即可食用。 
吃法:每日2碗,可于早上10点钟及 下午3点各吃一碗。 
8、麻油鸡: 
要买老母鸡的全鸡,也就是鸡的头尾脚都要一起烹煮,不可丢弃。 
材料: 每100公克的鸡肉,要老姜10公克`米酒100cc`麻油10 cc,以次类推。 
做法: 锅加热,麻油倒入,油热后,加入切片的老姜(不削皮),直到出香味但姜没有煎为止,把姜离移至锅的一侧,把切块的全鸡放下锅中炒,直到鸡肉7分熟后,把备 好的酒由锅的四周往中间淋,全部倒入后,盖锅煮,酒开了,即转为小火,再煮30-40分钟就好。 
吃法: 趁热吃,把浮油捞起,剩下的酒汤则置于热水瓶中,慢慢喝完。肉则配饭一起吃。 
9、花生猪蹄:(奶水不足者,在产后第三周可以食用) 
材料(三日份):花生,去膜去芽,虾,猪蹄约、带皮老姜适量、去柄香菇15克,米酒水2500CC,麻油80CC。 
做法:香菇在10倍量 的米酒水里泡软、切丝待用。花生放入米酒水中滚开,去膜,去芽。麻油加热,放老姜爆透。猪蹄放入锅中炒到外皮变色。放入花生炒一会儿,再把猪蹄和老姜放 进,最后加香菇、虾及米酒水。加盖烧滚,慢火炖约8小时。(这个时间有点长,可酌减。) 
10、油饭: 
材料(五日份):糯米, 去柄香菇、红萝卜、五花肉、虾米、 
带皮老姜适量,麻油适量、米酒水1000CC。 
做法:糯米用米酒水洗干净,滤干。将洗过的 糯米加入冷的米酒水中泡8小时后沥干,泡过的水要另外置于容器内备用,不要倒掉。米酒水须盖过糯米。将去柄香菇和虾米泡进刚才剩下的水中。泡软后香菇切 丝。带皮老姜与五花肉、红萝卜都切成粗丝。锅加热后放入四大匙麻油,将老姜炒成浅褐色。加虾米、香菇、五花肉、红萝卜、炒香取出。锅内重新加热,放三大匙 麻油,加热,下糯米炒到有黏性时,再加其他材料一起炒。将炒好的材料装进蒸锅,加泡过香菇、虾米的米酒水,分量盖过所有材料。蒸熟就可以吃了。 
11、素炖品: 
适量香菇、莲子、红枣、枸杞、山药,加米酒水炖至酥烂。以上材料可单独使用,也可一起使用。 
五、餐点用法(按周 计划) 
第一周(1——7天) 
早餐空腹:生化汤100CC(一杯分三次喝) 
早餐:麻油猪肝一碗、薏仁饭一碗。 
10点:红豆汤一碗。 
午餐空腹:生化汤100CC(一杯分三次喝) 
午餐:麻油猪肝一碗、薏仁饭一碗。 
3点: 糯米粥一碗。 
晚餐空腹:生化汤100CC(一杯分三次喝) 
晚餐:素炖品一碗。 
晚上点心:红豆汤一碗。
禁忌食物: 生冷食物,蒟弱,白萝卜,咸菜,腌渍白菜,梅干,味增汤、日本茶,乌龙茶,啤酒,果汁,牛乳,水,开水。醋,红花油,猪油,牛油 
第二周(8——14天) 
早餐:麻油腰子一碗,蔬菜一碗,糯米粥一碗,薏仁饭一碗,杜仲粉一钱。 
10点:红豆汤1碗。 
午餐:麻油腰子一碗、蔬菜一碗、薏仁饭一碗,杜仲粉一钱。 
3点:油饭一碗。 
晚餐:鱼汤一碗、杜仲粉一钱,素炖品一碗、杜仲 粉一钱。 
晚上点心:红豆汤一碗。 
禁忌食物:生冷食物,蒟弱,白萝卜,咸菜,腌渍白菜,梅干,味增汤、日本茶,乌龙茶,啤酒, 果汁,牛乳,水,开水。 
第三周——第四周(15——30天或40天) 
早餐:麻油鸡一碗、糯米粥一碗、薏仁饭一碗、水果一份。 
10点:红豆汤一碗。 
午餐:麻油鸡一碗、蔬菜一碗,水果一份。 
3点:花生猪蹄或素炖品一碗 
晚 餐:鱼汤或素炖品一碗、蔬菜一碗、薏仁饭一碗。 
晚上点心:油饭一碗。 

禁忌食物:生冷食物,蒟弱,白萝卜,咸菜, 腌渍白菜,梅干,味增汤 
说明:
1、从第二周开始,可吃少量蔬菜,但须选择比较温和的蔬菜,并尽量以红色蔬菜为主,如红萝卜、米 茜。第三周后可适量放宽,温和性蔬菜都可以。也须用米酒水烹 调。 
2、水果以哈密瓜,桃子,葡萄,木瓜为宜。 
3、产后所有的 食物及饮料,都要吃得温热,不能冷吃。(水果除外) 
4、以上的"一碗"的量并不精确,具体操作中参照餐点制作法的精确数量。 
六、 坐月子的生活 
1、安静休养30-40天 
产后最重要的一件事即为「休息」,在这段期间内,产妇周围的亲戚,如娘家的母亲与姊 妹、夫家的亲属、当然还有丈夫等,都应同心协力的来照顾产妇,不让她离开房间、不让她起身做任何劳动、不分贫富、或者第几次生产,甚至是小产,都一定要同 样的慎重!自然生产者须休养三十天,剖腹产、自然流产或人工流产者,更须延长休养的天数至四十天以上! 
2、卧床二周 
产后二周 内为子宫收缩最快速的时候,此时因怀孕时子宫被胎儿撑得非常大,一但生产,子宫成为真空状态,内脏因不再受压迫而变的非常松垮,若产后即常坐起或走动,因 地心引力的关系,将造成松垮的子宫及内脏收缩不良,引起内脏下垂,而「内脏下垂」就可能是所有妇女病的根源。所以产后二周内,除了吃饭及上厕所之外,其余 时间,不论是白天或是晚上,均应卧床休息。 
3、勤绑腹带防止「内脏下垂」并「收缩腹部」 
利用生产的机会来调整体型,或者改善 身体上的一些症状,是一个很重要的时机,所以很多人会在这段期间用纱布条绑腹,达到调整体型的目的! 

坐月子期间必须特别注意 防止「内脏下垂」,因内脏下垂可能为所有「妇女病」及「未老先衰」的根源,并会因此而产生小腹,故在坐月子期间须勤绑腹带以收缩腹部并防止内脏下垂,而若 原本即为内脏下垂体型者,亦可趁坐月子期间勤绑腹带来改善。。
所 使用的腹带为一条很长的白纱带,长约1200公分,宽为15公分,每人须准备二条 以便替换。因产后须热补,容易流汗,若汗湿时应将腹带拆开,并将腹部擦干,再洒些不带凉性的痱子粉后重新绑紧。若汗湿较严重时,则须更换干净的腹带,绑法 请参考「腹带的绑法」。如果使用一般一片粘的束腹或束裤,不仅没有防止内脏下垂的效果,反而有可能压迫内脏令气血不通畅,使内脏变形或产生胀气而造成呼吸 困难或下腹部突出的体型,请特别注意!
腹带的绑法 
1)尺寸:所使用的腹带为透气的白纱布,长1200公分,宽15公分 
2)用量:为产妇自己的功课,因为不穿衣裤(先绑好腹带后再将内裤穿上),平贴皮肤,容易汗湿,每人均需准备2条来替换 
3)功能:.a. 防止内脏下垂(一般束腹不适用) b.收缩腹部,消肚子 
4)开始绑的时间:自然产——产后第2天;剖腹产——第6天(5天内用束腹);小产 —— 术后第二天 
5)每日拆卸,重绑时间:三餐饭前须拆下、重新绑紧再吃饭;擦澡前拆下,擦澡后再绑上;产后2周24小时绑着,松了就重 绑;第3周后可白天绑,晚上拆下 
6)清洗方式:用冷洗精清洗,再用清水过净后晾干即可,勿用洗衣机,因易皱 
7)腹带的绑法及 拆法: 
a. 仰卧、平躺,把双膝竖起,脚底平放床上,膝盖以上的大腿部分尽量与腹部成直角;臀部抬高,并于臀部下垫2个垫子 
b. 两手放在下腹部,手心向前,将内脏往「心脏」的方向按摩、抱高 
c. 分2段式绑,从耻骨绑至肚脐,共绑12圈,前7圈重迭缠绕,每绕1圈半要「斜折」一次(斜折即将腹带的正面转成反面,再继续绑下去,斜折的部位为臀部两 侧),后5圈每圈往上挪高2公分,螺旋状的往上绑,最后盖过肚脐后用安全别针固定并将带头塞入即可 
d. 每次须绑足12圈,若腹围较大者须用3条腹带接成2条来使用 
e. 太瘦,髋骨突出,腹带无法贴住肚皮者,须先垫上毛巾后再绑腹带 
f. 拆下时须一边拆、一边卷回实心圆统状备用 
4、严禁洗头,但需用正确的方法清洁头皮 
怀孕期间子宫增加的负担是不可言喻 的,单看之前与之后子宫的大小就知道,因此,在生产后要使子宫尽快恢复原状。而要想子宫尽快的恢复功能,最重要的便是要将子宫内的污血完全排出,如果能使 子宫成为真空状态,则贺尔蒙的分泌将会特别活跃,子宫的功能亦会比怀孕前更好!而洗头,将会严重影响产后恶露的排除,只要头皮一受凉,子宫里的污血马上会 凝结成血块不易排出,就算马上吹干也不允许,况且用吹风机来吹头,是很容易引起头风及头痛的。而子宫内的废血若不清除干净,就很有可能会造成荷尔蒙不平衡 以及内分泌不
协调,进而产生许多并发的症状,所以产后三十天须绝对遵守不要洗头,以免后患无穷! 
然而产妇的新陈代谢特别旺盛,所以 必须用正确的方法来清洁头皮,方法如下: 

# 清洁头皮法 
将药用酒精隔水温热,再以脱脂棉花沾湿,将头发分开,前 后左右擦拭头皮,稍用手按摩一下头部后,再以梳子将脏物刷落,如此将会感到较清爽,此法可于饭前天天擦拭,或用软梳梳理头发,好让头部气血畅通,保持脑部 清新。 
5、二周内不可洗澡,但需用正确的方法擦澡 
为了避免受凉,产后两周内不可洗澡,但要用正确的方法擦澡,第三周起可淋 浴,满月后方可泡澡。 
# 擦澡的方法 
用烧开的水及米酒水各半,加入10cc的药用酒精及10公克的盐,掺和着成为擦澡水,用 毛巾沾湿、扭干,替产妇擦拭她的肚子及流汗的地方,早上、中午、晚上各一次,若冬天非常寒冷时,则一次就好。擦拭干净后还要抹上不带凉性的痱子粉,肚子上 如果绑上腹带,腹带也要适时的更换。 
脸部的清洁与保养 

洗脸及刷牙不需用药用酒精及盐巴,但需用温热的水,为预 防头风或头痛,绝不能用冷水;另外,脸部的保养,可以使用适合自己的洗面奶及保养品。 

局部的消毒 
可以将茶水(即 泡茶将茶叶滤掉的茶水)放入适量的盐巴与药用酒精混合使用,再用这样的水来清洗ying部及肛m,有收敛的作用。 
6、不可吹风,不论是热风 或冷风 
产后全身的毛细孔,包括头皮的毛细孔都是张开的,此时一吹到风,不论是热风或冷风,毛细孔就会立刻收缩,很容易造成筋骨酸痛、头痛、 头风,甚至感冒。 
7、要有舒适的环境,室温维持在摄氏25-28度 
产妇要有舒适的环境,所以夏天太过炎热、或者冬天太冷,均 需开空调让室温维持在摄氏25-28度之间,但却要注意不可以吹到风!所以一定要想办法将空调的风完全挡住,不可对着产妇吹,而且产妇须穿长裤、长袖、戴 帽子、手套、围巾,并且穿袜子来挡风! 
8、不可碰冷水 
产妇不可碰冷水,以防受凉或产生酸痛的现象,所以举凡擦澡、洗脸、洗 手、刷牙或产后第三周以后的冲澡,均需使用热水。 
9、不可抱小孩 
产后最重要的工作无它,就是安心下来,尽情的吃和睡。此时全 身的机能均在迅速的恢复中,所以当然不可提重物,更不可抱小孩,否则极易产生内脏下垂的体型。况且,新生的婴儿,骨骼、内脏均尚未发育完全,最好还是尽量 让他睡觉,常抱他只会对他造成不良影响。 
10、侧躺喂奶 
至于喂母奶时,也要侧躺在床上,将婴儿放于身侧让他吸奶,产妇可以斜 靠,并在婴儿及产妇的背面各放一个大枕头支撑,但要注意不要堵住婴儿的鼻子,以免窒息。 
11、关于母乳 
每一个女人于生产完 后,一定都会分泌母乳,母乳的分泌应是很充足的,但若不给婴儿吸食,就无法再分泌出来,即使婴儿一次就全部吸光,母乳的供应仍是源源不绝,因为这是母体的 本能。所以若不给婴儿吃母乳,当然是很不好的。若是婴儿的吃奶量很少,则应将每次剩下的母乳都充分挤掉,以刺激下次r房分泌足够的乳量。 

产妇若因故临时不亲自喂奶,也要把积存于乳房中的奶挤掉,旧奶积存于r房会使乳房产生硬块或导致乳腺炎,最好在产后的六个月中都能充分的授乳,这是最顺 乎自然的育儿原则,不但能保护母亲,而且可减少日后发生乳癌的机会。 
如果奶水清淡或不足,不妨于产后第三周起补充花生猪脚;而为了要让产后奶水 快速分泌,可于产后第一时间施行"按电铃"(刺激r头)的功课。 
# 按电铃(刺激乳头)法 
a.产后休息恢复后(剖腹产等麻药 退干净后)即开始每四个小时按一次电铃(刺激r头),直至奶水冲出来为止。
b.刺激r头的方法有三种:1.让刚出生的婴儿吸允 2.使用吸奶器 3.请新手爸爸协助以便控制吸力。 
c.注意:每次每边r房不要超过15分钟,但要固定每四小时刺激一次r头,不要间断,直 至奶水冲出来源源不绝为止。 
12、不可替小孩洗澡 
前面强调月子期间不可抱小孩,相同的道理当然更不可以弯着腰来替小宝贝洗澡,如果无法做到,那么产后腰酸背痛及手脚酸麻的现象必定会随之而来,所以最好在 产前就安排好小婴儿的照顾,或者跟先生商量,产后由先生来帮小贝比洗澡,如此还能增进亲子之间的感情呢! 
13、不提重物,不爬楼梯 
产后半年内均不可提太重的物品,以避免内脏下垂而导致腰酸背痛,而于月子期间爬楼梯更应禁止。 
14、不可流泪 
女性的老化从 眼睛开始,所以产后眼部的保养是非常重要的。产妇严禁流泪,俗云:「产妇一滴泪比十两黄金还贵重」,所以伤感的事,如亲朋好友亡故等不幸的事情,绝不能让 产妇知道,不能让她流泪,做丈夫的也应该在此时扛起所有的责任,让产妇能安心静养。 

产妇如果哭泣的话,眼睛会提早老化,有时 会演变为眼睛酸痛、青光眼或白内障的起因。当然,产妇本人也要尽量努力使自己心情开朗,不要担心杂事,要常常微笑,保持心情愉快。 
15、不 可看电视及书报杂志 
产妇应尽量少看电视及书报,如果一定要看,则每十五分钟须让眼睛休息十分钟。最好能多听听轻柔的音乐,一方面让眼睛充分的休息,一方面可调整情绪,消除神 经紧张。 
16、眼部按摩 
眼睛容易疲劳的产妇,可于三餐饭前及睡前将毛巾沾上热水(可稍热些),拧干后以毛巾热敷于眼部数分钟,再施行眼部按摩。 
# 眼部按摩法 
a.闭上眼睛,张开双肘,将双手中指从鼻梁由下往上推放在额中间的发际。 
b.以拇指腹放在眉头下凹处,用力压、揉,但不能压到眼珠。 
c.两中指仍维持往下压在发际,拇指渐向两侧按压,直到眼尾上方。 
进行眼睛指压以躺卧最为理想,如果不方便,也可以坐在椅子上进行,压揉 眼睛时须咬紧牙根,收缩下巴,颈后要用力。如果眼睛疲劳,压起来会有痛觉,但仍要继续指压,直到不痛为止。 
那个喝米酒 绑腹带 是小S坐月子期间做的呢
学习一下

七、产前各项准备(落实到人头)

1、 家属负责购买各种药材,包括生化汤的材料及月子饮料的材料。具体如下:

A.生化汤材料:当归(全),川穹,桃仁(去芯),烤老 姜,炙草(蜜甘草)。按以上方子购买,1X14份,分别包好。(本来7份就够,但为预防忽然决定剖腹产之需,得多准备)
B.杜仲粉:21份, 分别包好。
C.山楂肉、荔枝壳、观音串,各分3份包好。
以上物品在中药店有售,如果没有,请到医院找熟人开。请提前2个月备齐 (以防早产)


2、 准备腹带,到超市购买米酒、麻油、黑糖、薏仁、莲子、红枣、枸杞、山药、糯米、桂圆、量杯、煮酒锅等。

3、 生产当日,和老公先去医院,家人在家准备产后第一天所需的饮料、食物等,用容器装好,以便生产后立刻能用上。

计:生化汤,米酒 水、月子饮料、麻油猪肝,薏仁饭,糯米粥,以及所有家人的食物,可多备一点,在医院微波炉里转转吃。


八、总结做月子 要点:

1、月子里一定要吃的淡,能不放盐和味精就不放,因为宝宝的肾脏还没发育完全,这对奶水的质量和妈妈的体重都有直接关系, 快坚持不下去时,想到自己的身材和宝宝,也就忍啦

2、前两周以排毒为主(猪肝和腰子有助排毒),一定不能吃大补和大热的东东(如 桂园和蹄膀等),因为新妈妈经过生育,整个人非常虚弱,这时候大补不但吸收不到精华,且只会长膘,而且这时候长得膘很难减掉,热性的食物对奶水也不好,宝 宝吃了容易便秘;

3、尽量用麻油炒菜,麻油有削减脂肪的功效,比一般食用油不长脂;

4、水果一定不能 忘,但是前两周也不能大补,道理和2是一样的,多吃些哈密瓜、木瓜、葡萄等,西瓜什么的不要急着吃,因为西瓜太凉性;

5、任何冷 的流质食物都要加热后再吃,否则不利于子宫归位,不能刺激肠胃;

6、第4周开始可以补力气了(如鸡肉等),第二个月开始和大家一 样吃,但还是要注意清淡,否则宝宝受不了,自己也容易肿;

7、任何要加糖的点心食物都采用红糖,有助于恶露排出;

8、月子里早餐主食是糯米稀粥;中餐主食是薏米饭(薏米有排水消肿的效果的)不吃大米;晚餐主食是鱼汤。鱼汤(老姜用麻油爆的起皮后再煎鱼块,鱼用肉质 松软的鱼,加水煮熟,到鱼汤变白即可),淡的,奶水好的小秘诀哦;

9、水份的补充:多喝豆浆,多吃冬瓜对无论是孕期还是产妇都有 减肥和减肿得功效,还能提高奶水的质量;米酒煮开代替水喝,将米酒煮开之后,让酒精全部蒸发掉即可

Facebook Connect Tutorial

Facebook Connect Tutorial

Contents

Bookmark and Share

Author's Note: 01/22/2011

This tutorial is now out of date. Facebook has implemented a new way to do all of this, and this tutorial shows the "old" way. It will however still work, so I'm leaving it up here for now. Hopefully I can update it at somepoint to the new techniques. -P.G.

Why are we here?

This tutorial will focus on integrating Facebook Connect with a dynamic web site. The tutorial assumes it's an existing dynamic site that has its own user base and login mechanisms, but it applies equally to one that you are building from scratch if you still want to support more than one login mechanism (your own and Facebook Connect).

This tutorial is for you if

  • You want to make changes to (or build) a dynamic site that to allows people to login into using their Facebook account instead of registering and logging in with an account on the site.
  • You want to support users that already have an account on the site by linking their Facebook account id to the existing account.
  • You want to automatically login users when possible using Facebook login.
  • You want to render some Facebook content (profile picture, user information, etc).
  • Your site is written in PHP (or you're willing to deal with a PHP tutorial even though your site uses a different language).
  • The existing documentation is not giving you what you need, or you want some further examples.

Note

Before trying to implement on your site, you'll need to take care of the following steps which are not covered in the tutorial.

Next->

The Overview page will give a brief description of each page we will be going over in the tutorial.

Tuesday, April 12, 2011

Facebook Graph API App Easy w/ PHP SDK - 印痕的日志 - 网易博客

Facebook Graph API App Easy w/ PHP SDK - 印痕的日志 - 网易博客

As promised, here is a post (similar to my Twitter API post) on using the Facebook API. There are many reason why one would want to access the Facebook API – maybe to create a mobile app that lets you post photos to your Facebook albums, or maybe you just want to show your last few Facebook status updates on your blog; what ever the reason may be, Facebooks Graph API mixed in with their PHP SDK makes it really easy to accomplish this.

Objectives

  • Setup our environment
  • Register an app on Facebook
  • Understand the authentication process and extended parameters
  • Understand Graph API
  • Retrieve our latest status updates
  • Add a new status update
  • Retrieve our photos from our albums
  • Add a new photo

Setup our Environment

You have two options here, you can access my github and download this project with all required files and library or you can follow this tutorial step by step – it’s really up to you. If you want to download all the files ahead of time, you can get them at this github repo. If you are going step by step, first create a folder (I’m calling mine facebook) in your server where you want this site, next create a folder named library in the root of this site, and finally add the following two files facebook.php and fb_ca_chain_bundle.crt in the library folder that you can get from the Facebook PHP SDK repo at: https://github.com/facebook/php-sdk/tree/master/src/.

Register an App on Facebook

I’m making the assumption you already have a facebook account and if you don’t, first create one. To create an app on Facebook, you first need to confirm your account by giving Facebook a phone number where they can send you a code. This may be a new process since I don’t remember having to do this a few months ago. Go to http://developers.facebook.com/setup and if your account is not verified, you’ll be prompted to add the information mentioned above. After verifying your accout, you’ll be taken to the ‘Create an Application’ page.
Create App
This is what the page should look like
Simply give your site a name and type in your site’s url and click create app. In my case I’m calling my app ‘Blog Test’ and my site would be ‘http://www.joeyrivera.com/’. Next you will be prompted to type in a captcha and continue. Now you are done setting up your app, you see a screen like the following with your app id and app secret:
App Created
Here is your app info


Monday, April 11, 2011

PHP邮件发送验证功能 | 皮特博客

PHP邮件发送验证功能 | 皮特博客:

第一步:建立数据库
把下面数据库复制粘贴在一个记事本,然后在PHPMyAdmin创建一个新的数据库并导入下面数据。
– 表的结构 `registered_members`

CREATE TABLE `registered_members` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default ”,
`email` varchar(65) NOT NULL default ”,
`password` varchar(65) NOT NULL default ”,
`country` varchar(65) NOT NULL default ”,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

– 导出表中的数据 `registered_members`

– ——————————————————–

– 表的结构 `temp_members_db`
CREATE TABLE `temp_members_db` (
`confirm_code` varchar(65) NOT NULL default ”,
`name` varchar(65) NOT NULL default ”,
`email` varchar(65) NOT NULL default ”,
`password` varchar(15) NOT NULL default ”,
`country` varchar(65) NOT NULL default ”
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

– 导出表中的数据 `temp_members_db`
第二步:建立数据库连接
请把下面代码复制到你的文本编编译器里面,并命名:config.php
$host=”Hostname”; // Host name
$username=”Username”; // Mysql username
$password=”Password”; // Mysql password
$db_name=”DB name”; // Database name
//Connect to server and select database.
mysql_connect(“$host”, “$username”, “$password”)or die(“cannot connect to server”);
mysql_select_db(“$db_name”)or die(“cannot select DB”);
?>
第三步:注册页面
请把下面代码复制到你的文本编辑器里面,并命名:signup.php


注册
用户名 :
邮箱 :
密码 :
国家 :



第四步:数据插入到数据和邮件发送
请把下面代码复制到你的文本编辑器里面,并命名:signup_ac.php
// table name
$tbl_name=temp_members_db;
// Random confirmation code
$confirm_code=md5(uniqid(rand()));
// values sent from form
$name=$_POST['name'];
$email=$_POST['email'];
$country=$_POST['country'];
// Insert data into database
$sql=”INSERT INTO $tbl_name(confirm_code, name, email, password, country)VALUES(‘$confirm_code’, ‘$name’, ‘$email’, ‘$password’, ‘$country’)”;
$result=mysql_query($sql);
// if suceesfully inserted data into database, send confirmation link to email
if($result){
// —————- SEND MAIL FORM —————-
// send e-mail to …
$to=$email;
// Your subject
$subject=”Your confirmation link here”;
// From
$header=”from: your name ”;
// Your message
$message=”Your Comfirmation link \r\n”;
$message.=”Click on this link to activate your account \r\n”;
$message.=”http://www.yourweb.com/confirmation.php?passkey=$confirm_code”;
// send email
$sentmail = mail($to,$subject,$message,$header);
}
// if not found
else {
echo “Not found your email in our database”;
}
// if your email succesfully sent
if($sentmail){
echo “Your Confirmation link Has Been Sent To Your Email Address.”;
}
else {
echo “Cannot send Confirmation link to your e-mail address”;
}
?>
第五步:验证
请把下面代码复制到你的文本编辑器里面,并命名:confirmation.php
// Passkey that got from link
$passkey=$_GET['passkey'];
$tbl_name1=”temp_members_db”;
// Retrieve data from table where row that match this passkey
$sql1=”SELECT * FROM $tbl_name1 WHERE confirm_code =’$passkey’”;
$result1=mysql_query($sql1);
// If successfully queried
if($result1){
// Count how many row has this passkey
$count=mysql_num_rows($result1);
// if found this passkey in our database, retrieve data from table “temp_members_db”
if($count==1){
$rows=mysql_fetch_array($result1);
$name=$rows['name'];
$email=$rows['email'];
$password=$rows['password'];
$country=$rows['country'];
$tbl_name2=”registered_members”;
// Insert data that retrieves from “temp_members_db” into table “registered_members”
$sql2=”INSERT INTO $tbl_name2(name, email, password, country)VALUES(‘$name’, ‘$email’, ‘$password’, ‘$country’)”;
$result2=mysql_query($sql2);
}
// if not found passkey, display message “Wrong Confirmation code”
else {
echo “Wrong Confirmation code”;
}
// if successfully moved data from table”temp_members_db” to table “registered_members” displays message “Your account has been activated” and don’t forget to delete confirmation code from table “temp_members_db”
if($result2){
echo “Your account has been activated”;
// Delete information of this user from table “temp_members_db” that has this passkey
$sql3=”DELETE FROM $tbl_name1 WHERE confirm_code = ‘$passkey’”;
$result3=mysql_query($sql3);
}
}
?>
注意:
如果没有邮箱服务器的,会出现如下面提示之类的东西:
Warning: mail() [function.mail]: Failed to connect to mailserver at “localhost” port 25, verify your “SMTP” and “smtp_port” setting in php.ini or use ini_set() in E:\wamp\www\mailverification\signup_ac.php on line 39
Cannot send Confirmation link to your e-mail address
不用慌,这说明你的数据已经被写进数据库了。
你可以用以下方法去通过验证:
先复制这个URL到你的浏览器:http://www.你的网站.com/confirmation.php?passkey=
然后到你的数据库里面把confirm_code复制粘贴在passkey=后面,并回车,会出现提示的:Your account has been activated!
恭喜你了!你的邮件验证系统就完成了!如果有什么问题的,欢迎随时留言,回以最快的速度解决你的问题。由于时间关系,英文就不做翻译了。

Zend_Form Check For Existing Email Address In Database

Zend_Form Check For Existing Email Address In Database

If you’re coding a registration form you will either want to check if a username or an email address is already present in the database. This is very easy to do, assuming you are familiar with using Zend_Form:



That’s all. NOTE: This might be obvious but you cannot chain the setMessage(…) function directly to the end of new Zend_Validate_Db_NoRecordExists(‘user’, ‘email’); 

 If you are unfamiliar Zend_Form, here’s a full example:

Password confirmation using Zend Form and Validators | Thoughts, Codes and Games

Password confirmation using Zend Form and Validators | Thoughts, Codes and Games

A while back i needed a password confirmation field for my users. I looked and found this neat little snippet. I cant remember who created it originally so i cant give credits to the person but its quite easy to use. Just add this to your library and create a new validator object like this.
class App_Validate_PasswordConfirmation extends Zend_Validate_Abstract {
const NOT_MATCH = 'notMatch';
protected $_messageTemplates = array(
self::NOT_MATCH => 'Password confirmation does not match'
);
public function isValid($value, $context = null) {
$value = (string) $value;
$this->_setValue($value);
if (is_array($context)) {
if (
isset($context['password_confirm']) &&
($value == $context['password_confirm'])) {
return true;
}
} elseif (is_string($context) && ($value == $context)) {
return true;
}
$this->_error(self::NOT_MATCH);
return false;
}
}
This would be in your form where you want to have password confirmation.
$passwordConfirmation = new App_Validate_PasswordConfirmation();
$password = $this->addElement('password', 'password', array(
'filters' => array('StringTrim'),
'validators' => array(
$passwordConfirmation,
array('Alnum'),
array('StringLength', false, array(6, 100)),
),
'class' => 'input-text',
'required' => true,
'label' => 'Password',
));
$password_confirm = $this->addElement('password', 'password_confirm', array(
'filters' => array('StringTrim'),
'validators' => array(
$passwordConfirmation,
array('Alnum'),
array('StringLength', false, array(6, 100)),
),
'class' => 'input-text',
'required' => true,
'label' => 'Confirm Password',
));
view raw snippet.php This Gist brought to you by GitHub.

Thursday, April 7, 2011

PHP ereg 正则表达式_景安数据李怀丽_新浪博客

PHP ereg 正则表达式_景安数据李怀丽_新浪博客

通常上, 正则表达式会用到以下的几个"符号":
^ - 代表字串前面一定要有这样的字, 如^http://, 代表前面一定要有http://
$ - 代表字串后面一定要有这样的字.
? (一个问号) - 它代表一个或没有字元
* (一个*) - 它代表没有或者更多前面的字元
. (一个句点) - 它代表任何一个字元
+ (一个加)
- 它代表至少一个或更多前面的字元
[xyz] - 它代表任何一个字元, 或x, 或y, 或z
[a-z] - 它代表任何一个字元, 由a至z
[[:alnum:]] - 代表由a至z, 0至9
[[:digit:]] - 代表由0至9

当使用正则表达式时, 必需注意ereg及eregi可大大不同喔~
ereg是非常敏感的 (意即大小字母都分得非常清楚), 而eregi则不是 (只要记着i代表case-insensitive就可以了)

这里举一些例子:

$regexp = eregi("a?c", "abc"); //-- 这是对的, 因?可以代表"一个"或没有字元
$regexp = eregi("a?c", "ac"); //-- 这也是对的, 因?可以代表一个或"没有"字元
$regexp = eregi("a?c", "a"); //-- 这样就不对了, 纵使有a, 可是还是要有c

$regexp = eregi("[abc]", "a"); //-- 这是对的, 因a是大括号里的其中一个字元
$regexp = eregi("[a-z]", "c"); //-- 这是对的, 因c包括在a至z里
$regexp = eregi("[a-z]", "0"); //-- 这是不对的, 因0并不包括在a至z里面

$regexp = eregi("a.c", "abc"); //-- 这是对的, 因一个.代表"任何"一个字
$regexp = eregi("a.c", "ac"); //-- 这可就不对了, 因.代表"一个"字元, 所以放abbc也是不对的

$regexp = eregi("a+c", "aaaac"); //-- 这是对的, 因+代表一人或"更多"前面的字元
$regexp = eregi("a+c", "abbc"); //-- 这样是不对的, +代表一个或更多"前面的字元", 而不是代表任何一个字元
$regexp = eregi("a+c", "abc"); //-- 这样也是不对的, 注释如上

$regexp = eregi("[^abc]", "a"); //-- 这是比较不同的一点, 那就是如果^出现在[]里面, 代表"除"了里面的字, 全部都是对的.
$regexp = eregi("[^abc]", "d"); //-- 这是对的, 因d并不在abc里面
$regexp = eregi("[^[:alnum:]]", "9"); //--这是对的, 因~并不包括在a至z, 0至9里面
当然, [[:digit:]]用法也是相同:
$regexp = eregi("[^[:digit:]]", "a"); //-- 这是对的, 因a并不包括在0至9里面

最后, 举个有用的例子,
比如说, 我要每个人的密码都不准拥有0至9, 可以这样试验:
$regexp = eregi("^[^0-9]+$", "aaa9"); //--把aaa9当做密码
if ($regexp == "") {
echo "密码拥有号码, 请改过";
} else {
echo "密码没有号码, 通过";
}

测验显示, 密码拥有号码, 请改过.
相信每个人都看到, [^0-9]前后各有^及$, 这是为了确保前后不能拥有号码, 而+则确保中间没有任何的号码,
[]里面的^, 则是"除了0至9之外, 其他一律通过"
所以只要将9拿去, 就会显示"密码没有号码, 通过"了 !

Using Regular Expressions with PHP

Using Regular Expressions with PHP

Regular expressions are a powerful tool for examining and modifying text. Regular expressions themselves, with a general pattern notation almost like a mini programming language, allow you to describe and parse text. They enable you to search for patterns within a string, extracting matches flexibly and precisely. However, you should note that because regular expressions are more powerful, they are also slower than the more basic string functions. You should only use regular expressions if you have a particular need.
This tutorial gives a brief overview of basic regular expression syntax and then considers the functions that PHP provides for working with regular expressions.
PHP supports two different types of regular expressions: POSIX-extended and Perl-Compatible Regular Expressions (PCRE). The PCRE functions are more powerful than the POSIX ones, and faster too, so we will concentrate on them.
The Basics
In a regular expression, most characters match only themselves. For instance, if you search for the regular expression "foo" in the string "John plays football," you get a match because "foo" occurs in that string. Some characters have special meanings in regular expressions. For instance, a dollar sign ($) is used to match strings that end with the given pattern. Similarly, a caret (^) character at the beginning of a regular expression indicates that it must match the beginning of the string. The characters that match themselves are called literals. The characters that have special meanings are called metacharacters.
The dot (.) metacharacter matches any single character except newline (\). So, the pattern h.t matches hat, hothit, hut, h7t, etc. The vertical pipe (|) metacharacter is used for alternatives in a regular expression. It behaves much like a logical OR operator and you should use it if you want to construct a pattern that matches more than one set of characters. For instance, the pattern Utah|Idaho|Nevada matches strings that contain "Utah" or "Idaho" or "Nevada". Parentheses give us a way to group sequences. For example, (Nant|b)ucket matches "Nantucket" or "bucket". Using parentheses to group together characters for alternation is called grouping.
If you want to match a literal metacharacter in a pattern, you have to escape it with a backslash.
To specify a set of acceptable characters in your pattern, you can either build a character class yourself or use a predefined one. A character class lets you represent a bunch of characters as a single item in a regular expression. You can build your own character class by enclosing the acceptable characters in square brackets. A character class matches any one of the characters in the class. For example a character class [abc] matches a, b or c. To define a range of characters, just put the first and last characters in, separated by hyphen. For example, to match all alphanumeric characters: [a-zA-Z0-9]. You can also create a negated character class, which matches any character that is not in the class. To create a negated character class, begin the character class with ^: [^0-9].
The metacharacters +, *, ?, and {} affect the number of times a pattern should be matched. + means "Match one or more of the preceding expression", * means "Match zero or more of the preceding expression", and ? means "Match zero or one of the preceding expression". Curly braces {} can be used differently. With a single integer, {n} means "match exactly n occurrences of the preceding expression", with one integer and a comma, {n,} means "match n or more occurrences of the preceding expression", and with two comma-separated integers {n,m} means "match the previous character if it occurs at least n times, but no more than m times".
Now, have a look at the examples:
Regular Expression Will match...
foo The string "foo"
^foo "foo" at the start of a string
foo$ "foo" at the end of a string
^foo$ "foo" when it is alone on a string
[abc] a, b, or c
[a-z] Any lowercase letter
[^A-Z] Any character that is not a uppercase letter
(gif|jpg) Matches either "gif" or "jpeg"
[a-z]+ One or more lowercase letters
[0-9\.\-] Аny number, dot, or minus sign
^[a-zA-Z0-9_]{1,}$ Any word of at least one letter, number or _
([wx])([yz]) wy, wz, xy, or xz
[^A-Za-z0-9] Any symbol (not a number or a letter)
([A-Z]{3}|[0-9]{4}) Matches three letters or four numbers
Perl-Compatible Regular Expressions emulate the Perl syntax for patterns, which means that each pattern must be enclosed in a pair of delimiters. Usually, the slash (/) character is used. For instance, /pattern/.
The PCRE functions can be divided in several classes: matching, replacing, splitting and filtering.
Back to top
Matching Patterns
The preg_match() function performs Perl-style pattern matching on a string. preg_match() takes two basic and three optional parameters. These parameters are, in order, a regular expression string, a source string, an array variable which stores matches, a flag argument and an offset parameter that can be used to specify the alternate place from which to start the search:
preg_match ( pattern, subject [, matches [, flags [, offset]]])
The preg_match() function returns 1 if a match is found and 0 otherwise. Let's search the string "Hello World!" for the letters "ll":

if (preg_match("/ell/", "Hello World!", $matches)) {
  echo "Match was found
";

  echo $matches[0];
}
?>


The letters "ll" exist in "Hello", so preg_match() returns 1 and the first element of the $matches variable is filled with the string that matched the pattern. The regular expression in the next example is looking for the letters "ell", but looking for them with following characters:
if (preg_match("/ll.*/", "The History of Halloween", $matches)) {
  echo "Match was found
";
  echo $matches[0];
}
?>
Now let's consider more complicated example. The most popular use of regular expressions is validation. The example below checks if the password is "strong", i.e. the password must be at least 8 characters and must contain at least one lower case letter, one upper case letter and one digit:
$password = "Fyfjk34sdfjfsjq7";

if (preg_match("/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/", $password)) {
    echo "Your passwords is strong.";
} else {
    echo "Your password is weak.";
}
?>

The ^ and $ are looking for something at the start and the end of the string. The ".*" combination is used at both the start and the end. As mentioned above, the .(dot) metacharacter means any alphanumeric character, and * metacharacter means "zero or more". Between are groupings in parentheses. The "?=" combination means "the next text must be like this". This construct doesn't capture the text. In this example, instead of specifying the order that things should appear, it's saying that it must appear but we're not worried about the order.

The first grouping is (?=.*{8,}). This checks if there are at least 8 characters in the string. The next grouping (?=.*[0-9]) means "any alphanumeric character can happen zero or more times, then any digit can happen". So this checks if there is at least one number in the string. But since the string isn't captured, that one digit can appear anywhere in the string. The next groupings (?=.*[a-z]) and (?=.*[A-Z]) are looking for the lower case and upper case letter accordingly anywhere in the string.

Finally, we will consider regular expression that validates an email address:
$email = firstname.lastname@aaa.bbb.com;
$regexp = "/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/";

if (preg_match($regexp, $email)) {
    echo "Email address is valid.";
} else {
    echo "Email address is not valid.";
}
?>
This regular expression checks for the number at the beginning and also checks for multiple periods in the user name and domain name in the email address. Let's try to investigate this regular expression yourself.
For the speed reasons, the preg_match() function matches only the first pattern it finds in a string. This means it is very quick to check whether a pattern exists in a string. An alternative function, preg_match_all(), matches a pattern against a string as many times as the pattern allows, and returns the number of times it matched.
Back to top
Replacing Patterns
In the above examples, we have searched for patterns in a string, leaving the search string untouched. The preg_replace() function looks for substrings that match a pattern and then replaces them with new text. preg_replace() takes three basic parameters and an additional one. These parameters are, in order, a regular expression, the text with which to replace a found pattern, the string to modify, and the last optional argument which specifies how many matches will be replaced.

preg_replace( pattern, replacement, subject [, limit ])

Autocomplete Example With Zend_Dojo_Form_Element_FilteringSelect And Zend_Dojo_Data

Autocomplete Example With Zend_Dojo_Form_Element_FilteringSelect And Zend_Dojo_Data

Zend Framework brings lot of user interface goodies with Zend_Dojo family of classes. In this article let us explore how to build a form element with autocomplete feature.
As a prerequisite you must be familiar with
  • Zend_Controller_Action
  • Zend_Layout
  • Zend_Form
Would it be nice if I tell you that you don't need any JavaScript knowledge? Zend_Dojo empowers PHP programmers to build dynamic and appealing forms without writing a single line of JavaScript.
This example has been tested with Zend Framework 1.7.0.
In this example, we will build a text element where the visitor can either select the user from the drop down list or type the username. While typing the username, the form element generates a drop down list filtering the data from user input. Take a look at the filteringSelect Dijit example to understand the type of form element we will be building.
FilteringSelect differs from Combobox Dojo widget in that, the value of the form element must be provided in the list. Also, you could display the username on the screen and set the 'user id' as the element value.
We will use the autoCompleteDojo action helper to send JSON data.
Let's start coding.
Step 1: Set up the Dojo environment in our bootstrap file.

// Create new view object if not already instantiated
//$view = new Zend_View();
Zend_Dojo::enableView($view);
$view->dojo()
->
addStyleSheetModule('dijit.themes.tundra')
->
setDjConfigOption('usePlainJson', true)
->
disable();

$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer'
);
$viewRenderer->setView($view);

?>

We enable Zend_Dojo in the $view object and set it to the view renderer.
In this example, we use the default CDN set in Zend_Dojo. You don't have to add Dojo JavaScript files to your web server. We add the 'tundra' stylesheet.
Step 2: Create the controller. Create the file DemoController.php in your controller directory and add the code below to it.


class DemoController extends Zend_Controller_Action
{
public function
indexAction()
{
}

public function
userlistAction()
{
}

public function
getForm()
{
}

}

?>

As you can see, it is just a skeleton of our controller. We add the code to it in a moment.
We build our form in the function getForm(). In a real world application you might want to create the form in a different class and file. In indexAction we display the form and process the data submitted by the user. In userlistAction we generate the data in JSON format which is required by the filteringSelect element.
Step 3: Create the form and elements. Put the below code in the getForm() function.
= new Zend_Form;

$userId = new Zend_Dojo_Form_Element_FilteringSelect('userId');
$userId->setLabel('Select a user')
->
setAutoComplete(true)
->
setStoreId('userStore')
->
setStoreType('dojo.data.ItemFileReadStore')
->
setStoreParams(array('url'=>'/demo/userlist'))
->
setAttrib("searchAttr", "username")
->
setRequired(true);

$submit = $form->createElement('submit', 'submit');

$form->addElements(array($userId, $submit));

return
$form;

?>
$userId is the form element containing the Dojo filteringSelect widget. We give the name userStore to our data store. We are specifying that we will serve the data from the URL /demo/userlist. Finally, we are setting the searchAttr to 'username'.
Step 4: Prepare the database.
CREATE TABLE `user` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `username` VARCHAR( 100 ) NOT NULL , ) ENGINE = InnoDB   INSERT INTO `zf`.`user` ( `id` , `username` ) VALUES ( NULL , 'jamey' ), ( NULL , 'hryan' ), ( NULL , 'jennyross' ), ( NULL , 'natebrg' ), ( NULL , 'deric' );

Create a sample table and add dummy data to it.
Step 5: Fetch the data from the database, convert to JSON format and send. I use the MySQL database 'zf' in this example. Insert below code to userlistAction(). I am connecting to the database from within the action in this example. Practically, you would want to connect to the database in a centrally accessible place like the bootstrap or a front controller plugin.
= new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => '127.0.0.1',
'username' => 'zf',
'password' => 'password',
'dbname' => 'zf'
));

$result = $db->fetchAll("SELECT * FROM user");
$data = new Zend_Dojo_Data('id', $result);
$this->_helper->autoCompleteDojo($data);
?>
It takes only three lines to fetch the data and send it in the format Dojo requires. We pass two parameters - id and $result the Zend_Dojo_Data constructor. 'id' is the unique identifier in our database table. The autoCompleteDojo action helper is convenient to use. It disables the layout and viewrenderer. It sets the appropriate headers and sends the data using the Zend_Dojo_Data object.
Point your browser to demo/userlist, you should receive the data in a file. Dojo uses this file via dojo.data.ItemFileReadStore.
Step 6:Set up indexAction(). Add the below code to the indexAction() function.

= $this->getForm();

if (
$this->_request->isPost()) {

if (
$form->isValid($_POST)) {
/*
* Process data
*/
$userId = $this->_getParam('userId');
//$userId contains the userId input by the user
} else {
$form->populate($_POST);

$this->view->form = $form;
}

} else {

$this->view->form = $form;
}

?>
Step 7:Set up the view and layout
In the view script index.phtml write:

<h1>Demoh1>
php
echo $this->form;
?>
In your layout file output the Dojo files and set the body tag to tundra class. When you echo $this->dojo(), links to Dojo JavaScript source files are printed. It also prints the JavaScript code that Zend Framework generates for you.
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

php
echo $this->dojo()->addStylesheetModule('dijit.themes.tundra');
?>






echo $this->layout()->content ?>




?>
Initially it may appear that you have to write a lot of code to get this work. Once you understand the whole process it becomes trivial to build form elements with autocomplete feature.
I have attached the files to this post for your reference.
You might also want to take a look at:
Add A Cool Zend Dojo Date Picker Form Element Without Writing A Single Line Of JavaScript
AttachmentSize
zf-filteringSelect-example.tar_.gz11.6 KB