登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

樱之花

叶散的时候,你明白欢聚;花谢的时候,你明白青春.

 
 
 

日志

 
 
关于我

分类中“我的实验室”是我在日常工作中的一些知识总结,有些写的比较匆忙,可能大家在阅读时会产生困扰,后期有时间我会重新整理编辑,谢谢大家的到访,您们的支持是我前进的动力!

PHP表单提交字符转换  

2007-09-25 13:36:45|  分类: PHP开发 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

nl2br

string nl2br ( string $string )

Returns string with '<br />' inserted before all newlines.

注意: Starting with PHP 4.0.5, nl2br() is now XHTML compliant. All versions before 4.0.5 will return string with '<br>' inserted before newlines instead of '<br />'.

例 2236. using nl2br()

<?php

echo nl2br("foo isn't\n bar");

?>

this will output :

foo isn't<br /> bar

 

htmlspecialchars

string htmlspecialchars ( string $string [, int $quote_style [, string $charset]] )

Certain characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function returns a string with some of these conversions made; the translations made are those most useful for everyday web programming. If you require all HTML character entities to be translated, use htmlentities() instead.

This function is useful in preventing user-supplied text from containing HTML markup, such as in a message board or guest book application. The optional second argument, quote_style, tells the function what to do with single and double quote characters. The default mode, ENT_COMPAT, is the backwards compatible mode which only translates the double-quote character and leaves the single-quote untranslated. If ENT_QUOTES is set, both single and double quotes are translated and if ENT_NOQUOTES is set neither single nor double quotes are translated.

The translations performed are:

  • '&' (ampersand) becomes '&amp;'
  • '"' (double quote) becomes '&quot;' when ENT_NOQUOTES is not set.
  • ''' (single quote) becomes '&#039;' only when ENT_QUOTES is set.
  • '<' (less than) becomes '&lt;'
  • '>' (greater than) becomes '&gt;'

例 2229. htmlspecialchars() example

<?php

$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);

echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;

?>

 

 addcslashes

string addcslashes ( string $str, string $charlist )

返回字符串,该字符串在属于参数 charlist 列表中的字符前都加上了反斜线。如果 charlist 中包含有 \n\r 等字符,将以 C 语言风格转换,而其它非字母数字且 ASCII 码低于 32 以及高于 126 的字符均转换成使用八进制表示。

当选择对字符 0,a,b,f,n,r,t 和 v 进行转义时需要小心,它们将被转换成 \0,\a,\b,\f,\n,\r,\t 和 \v。在 PHP 中,只有 \0(NULL),\r(回车符),\n(换行符)和 \t(制表符)是预定义的转义序列, 而在 C 语言中,上述的所有转换后的字符都是预定义的转义序列。

charlist 参数,如“\0..\37”,将转义所有 ASCII 码介于 0 和 31 之间的字符。

ord() 函数获取字符的 ASCII 码值。

<?php

echo addcslashes("zoo['.']", 'z..A');

// 输出:\zoo['\.']

?>

 

stripslashes 

(PHP 4, PHP 5)

stripslashes — Un-quote string quoted with addslashes()

Description

string stripslashes ( string $str )

Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\\) are made into a single backslash (\).

注意: If magic_quotes_sybase is on, no backslashes are stripped off but two apostrophes are replaced by one instead.

An example use of stripslashes() is when the PHP directive magic_quotes_gpc is on (it's on by default), and you aren't inserting this data into a place (such as a database) that requires escaping. For example, if you're simply outputting data straight from an HTML form.

例 2268. A stripslashes() example

<?php

$str = "Is your name O\'reilly?";

// Outputs: Is your name O'reilly?

echo stripslashes($str);

?>

注意: stripslashes() is not recursive. If you want to apply this function to a mutli-dimensional array, you need to use a recursive function.

例 2269. Using stripslashes() on an array

<?php

function stripslashes_deep($value)

{

    $value = is_array($value) ?

                array_map('stripslashes_deep', $value) :

                stripslashes($value);

    return $value;

}

// Example

$array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"));

$array = stripslashes_deep($array);

// Output

print_r($array);

?>

上例将输出:

Array( [0] => f'oo [1] => b'ar [2] => Array ( [0] => fo'o [1] => b'ar ))

 

addslashes

(PHP 4, PHP 5)

addslashes — 使用反斜线引用字符串

说明

string addslashes ( string $str )

返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线。这些字符是单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)。

一个使用 addslashes() 的例子是当你要往数据库中输入数据时。例如,将名字 O'reilly 插入到数据库中,这就需要对其进行转义。大多数据库使用 \ 作为转义符:O\'reilly。这样可以将数据放入数据库中,而不会插入额外的 \。当 PHP 指令 magic_quotes_sybase 被设置成 on 时,意味着插入 ' 时将使用 ' 进行转义。

默认情况下,PHP 指令 magic_quotes_gpcon,它主要是对所有的 GET、POST 和 COOKIE 数据自动运行 addslashes()。不要对已经被 magic_quotes_gpc 转义过的字符串使用 addslashes(),因为这样会导致双层转义。遇到这种情况时可以使用函数 get_magic_quotes_gpc() 进行检测。

例 2210. addslashes() 例子

<?php

$str = "Is your name O'reilly?";

// 输出:Is your name O\'reilly?

echo addslashes($str);

?>

  评论这张
 
阅读(1354)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018