Blog
网页中的字符串的处理[输出篇]
发布时间: 2009-01-19 14:13
ThinkPHP中的字符串的处理[输入篇]介绍了ThinkPHP的输入注意的事项,下面,我们介绍输出。
仍旧使用上次插入的数据。
- public function show()
- {
- $model = D('test');
- $data = $model->find(2);
- dump($data);
- }
[separator]
输出结果:
- array(3) {
- ["id"] => string(1) "2"
- ["title"] => string(15) "ThinkPHP's Code"
- ["content"] => string(66) "I like ThinkPHP.
- <b>me too!b>
- it's good!"
- }
下面把代码修改为:
- public function show()
- {
- $model = D('test');
- $data = $model->find(2);
- echo '$data['title'].'">'.$data['title'].'';
- }
结果出来一个正确的超链接。
但是,在实际情况中,html标签的属性值有可能是单引号,所以为了适应所有的情况,我们使用Input::forTag();
- echo '$data['title']).'">'.$data['title'].'';
输出的html代码将是:ThinkPHP's Code
同样,如果这个标题ThinkPHP's Code,更改成 "论Html中的"(不包括引号),连接就会不正常了。
所以,对于纯文本形式的输出,我们需要使用:Input::forShow();
- echo '$data['title']).'">'.Input::forShow('论Html中的').'';
输出的html代码是:
- <a href="#" title="ThinkPHP's Code">论Html中的<a>a>a>
对于多行文本,我们再试一下:
- echo '$data['title']).'">'.Input::forShow($data['content']).'';
得到的html代码是:
- <a href="#" title="ThinkPHP's Code">I like ThinkPHP.<br /><b>me too!b><br />it's good!a>
在浏览器中,保持了原来的换行的格式。
数据有输入就有编辑:
在纯文本的编辑中:
echo '<textarea>'.Input::forTarea($data['content']).'</textarea>';
得到的就是在输入的时候同样的格式,forTarea,就是专门在<textarea>中显示数据用的。
而单行纯文本的编辑,是在<input value="数据" /> 进行的,所以使用forTag就可以了。
下面看看可视化编辑器吧。FCKeditor是这个样子
<textarea type="hidden" id="FCKeditor" name="FCKeditor" style="display:none" >数据</textarea><input type="hidden" id="FCKeditor___Config" value="" style="display:none" /><iframe id="FCKeditor___Frame" src="FCKeditor/editor/fckeditor.html?InstanceName=FCKeditor&Toolbar=Default" width="100%" height="500" frameborder="0" scrolling="no"></iframe>
也就是说,需要编辑的数据是在textarea标签的内部。经过测试,只需要进行forTarea就可以了。从前我记得FCKeditor编辑器是使用input的,对于这种编辑器,forTag就可以解决问题。
我们总结一下,ThinkPHP输出数据的时候的原则:
* 页面显示:
* 纯文本方式显示在网页中,如文章标题<title>$data</title>: $data = Input::forShow($field);
* HTML 在网页中显示,如文章内容:无需处理。
* 在网页中以源代码方式显示html,也属于纯文本方式显示:$vo = Input::forShow($html);
* 纯文本或者HTML在textarea中进行编辑: $vo = Input::forTarea($value);
* html在标签中使用,如<input value="数据" /> ,使用 $vo = Input::forTag($value); 或者 $vo = Input::hsc($value);
*
* 以上,你只需要记住forTarea,forTag就可以了。其他情况,只要纯文本显示,就是forShow,使用html无需处理。
。
最后说说一个特殊情况,在mysql的搜索中,%和_是特殊的字符,%代表任意字符,_代表一个字符。
如果一个标题是 %abc%efg 的话,应该使用\%abc\%efg来搜索。
不然 %abc%efg就会搜索出来 类似 SDR3432abc24323efg这种。
下面看看代码:
- import('ORG.Util.HashMap');
- import('ORG.Util.HashMap');
- $map = new HashMap();
- $model = D('test');
- $map->put('title',array('like','%abc%efg'));
- $data = $model->findAll($map);
看看搜索结果:
- array(2) {
- [0] => array(3) {
- ["id"] => string(1) "9"
- ["title"] => string(8) "%abc%efg"
- ["content"] => string(66) "I like ThinkPHP.
- <b>me too!</b>
- it's good!"
- }
- [1] => array(3) {
- ["id"] => string(2) "10"
- ["title"] => string(18) "SDR3432abc24323efg"
- ["content"] => string(66) "I like ThinkPHP.
- <b>me too!</b>
- it's good!"
- }
- }
明白了吧。记得搜索的时候,如果需要搜索%和_这两个特殊字符,记得使用Input::forSearch
- import('ORG.Util.HashMap');
- $map = new HashMap();
- $model = D('test');
- $map->put('title',array('like',Input::forSearch('%abc%efg') ));
- $data = $model->findAll($map);
搜索到的结果就是
- array(1) {
- [0] => array(3) {
- ["id"] => string(1) "9"
- ["title"] => string(8) "%abc%efg"
- ["content"] => string(66) "I like ThinkPHP.
- <b>me too!</b>
- it's good!"
- }
- }
可能有人问,这个有必要吗?如果%或者_这种特殊字符也可以搜索的时候,就有用。
到这里,关于ThinkPHP中的字符串的存贮,编辑,显示过程中的各种处理就分析完毕了。由于个人时间上的原因,写得比较仓促,还请见谅。
