PHP学习系列(1)——字符串处理函数(4)
16、hebrevc() 函数把希伯来文本从右至左的流转换为左至右的流。它也会把新行 (\n) 转换为 <br />。只有 224 至 251 之间的 ASCII 字符,以及标点符号受到影响。
语法:hebrev(string,maxcharline)
maxcharline规定每行的最大字符数。如果可能,hebrev() 将避免把单词断开。
提示:hebrev() 和 hebrevc() 可以把希伯来逻辑文本转换为希伯来可见文本。希伯来可见文本不需要特殊的右至左字符支持,这使它对于在 web 上显示希伯来文本很有用处。
17、htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体。
预定义的字符是:
- & (和号) 成为 &
- " (双引号) 成为 "
- ' (单引号) 成为 '
- < (小于) 成为 <
- > (大于) 成为 >
语法:htmlspecialchars(string,quotestyle,character-set)
quotestyle——可选。规定如何编码单引号和双引号。
- ENT_COMPAT - 默认。仅编码双引号。
- ENT_QUOTES - 编码双引号和单引号。
- ENT_NOQUOTES - 不编码任何引号。
character-set——可选。字符串值,规定要使用的字符集。
- ISO-8859-1 - 默认。西欧。
- ISO-8859-15 - 西欧(增加 Euro 符号以及法语、芬兰语字母)。
- UTF-8 - ASCII 兼容多字节 8 比特 Unicode
- cp866 - DOS 专用 Cyrillic 字符集
- cp1251 - Windows 专用 Cyrillic 字符集
- cp1252 - Windows 专用西欧字符集
- KOI8-R - 俄语
- GB2312 - 简体中文,国家标准字符集
- BIG5 - 繁体中文
- BIG5-HKSCS - Big5 香港扩展
- Shift_JIS - 日语
- EUC-JP - 日语
提示:无法被识别的字符集将被忽略,并由 ISO-8859-1 代替。
例子
- <html>
- <body>
- <?php
- $str = "John & 'Adams'";
- echo htmlspecialchars($str, ENT_COMPAT);
- echo "<br />";
- echo htmlspecialchars($str, ENT_QUOTES);
- echo "<br />";
- echo htmlspecialchars($str, ENT_NOQUOTES);
- ?>
- </body>
- </html>
浏览器输出:
- John & 'Adams'
- John & 'Adams'
- John & 'Adams'
如果在浏览器中查看源代码,会看到这些 HTML:
- <html>
- <body>
- John & 'Adams'<br />
- John & 'Adams'<br />
- John & 'Adams'
- </body>
- </html>
18、htmlspecialchars_decode() 函数把一些预定义的 HTML 实体转换为字符,是htmlspecialchars() 的反函数。
语法:htmlspecialchars_decode(string,quotestyle)
quotestyle的具体含义同htmlspecialchars()。
19、implode() 函数把数组元素组合为一个字符串。
语法:implode(separator,array)
separator——可选。规定数组元素之间放置的内容。默认是 ""(空字符串)。
array——必需。要结合为字符串的数组。
说明:虽然 separator 参数是可选的。但是为了向后兼容,推荐您使用使用两个参数。
注释:implode() 可以接收两种参数顺序。但是由于历史原因,explode() 是不行的。你必须保证 separator 参数在 string 参数之前才行。
例子
- <?php
- $arr = array('Hello','World!','Beautiful','Day!');
- echo implode(" ",$arr);
- ?>
输出:
- Hello World! Beautiful Day!
- 20、join() 函数把数组元素组合为一个字符串。join() 函数是 implode() 函数的别名。
- 21、levenshtein() 函数返回两个字符串之间的 Levenshtein 距离。
Levenshtein 距离,又称编辑距离,指的是两个字符串之间,由一个转换成另一个所需的最少编辑操作次数。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。
例如把 kitten 转换为 sitting:
- sitten (k→s)
- sittin (e→i)
- sitting (→g)
levenshtein() 函数给每个操作(替换、插入和删除)相同的权重。不过,您可以通过设置可选的 insert、replace、delete 参数,来定义每个操作的代价。
语法:levenshtein(string1,string2,insert,replace,delete)
参数 | 描述 |
---|---|
string1 | 必需。要对比的第一个字符串。 |
string2 | 必需。要对比的第二个字符串。 |
insert | 可选。插入一个字符的代价。默认是 1。 |
replace | 可选。替换一个字符的代价。默认是 1。 |
delete | 可选。删除一个字符的代价。默认是 1。 |
注意:
如果其中一个字符串超过 255 个字符,levenshtein() 函数返回 -1。levenshtein() 函数对大小写不敏感。levenshtein() 函数比 similar_text() 函数更快。不过,similar_text() 函数提供需要更少修改的更精确的结果。
例子
- <?php
- echo levenshtein("Hello World","ello World");
- echo "<br />";
- echo levenshtein("Hello World","ello World",10,20,30);
- ?>
输出:
- 1
- 30
- 22、localeconv() 函数返回包含本地数字及货币信息格式的数组。
- 23、ltrim() 函数从字符串左侧删除空格或其他预定义字符。功能类似于chop()或者rtrim();
- 24、md5() 函数计算字符串的 MD5 散列。md5() 函数使用 RSA 数据安全,包括 MD5 报文摘译算法。如果成功,则返回所计算的 MD5 散列,如果失败,则返回 false。
语法:md5(string,raw)
raw——可选。规定十六进制或二进制输出格式:
- TRUE - 原始 16 字符二进制格式
- FALSE - 默认。32 字符十六进制数
注释:该参数是 PHP 5.0 中添加的。
25、md5_file() 函数计算文件的 MD5 散列。md5() 函数使用 RSA 数据安全,包括 MD5 报文摘译算法。如果成功,则返回所计算的 MD5 散列,如果失败,则返回 false。
例子 1
- <?php
- $filename = "test.txt";
- $md5file = md5_file($filename);
- echo $md5file;
- ?>
输出:
- 5d41402abc4b2a76b9719d911017c592
- 26、metaphone() 函数计算字符串的 metaphone 键。metaphone 键字符串的英语发音。metaphone() 函数可用于拼写检查应用程序。
- 如果成功,则返回字符串的 metaphone 键,如果失败,则返回 false。
语法:metaphone(string,length)
length——可选。规定 metaphone 键的最大长度。
说明:metaphone() 为发音相似的单词创建相同的键。所生成的 metaphone 键长度可变。metaphone() 比 soundex() 函数更精确,因为 metaphone() 了解基本的英语发音规则。
例子:
例子 1
- <?php
- echo metaphone("world");
- ?>
输出:
- WRLT
例子 2
在本例中,我们对两个发音相似的单词应用 metaphone() 函数:
- <?php
- $str = "Sun";
- $str2 = "Son";
- echo metaphone($str);
- echo metaphone($str2);
- ?>
输出:
- SN
- SN
- 27、money_format() 函数把字符串格式化为货币字符串。
语法:money_format(string,number)
number——可选。被插入格式化字符串中 % 符号位置的数字。
注释:money_format() 函数无法在 windows 平台上工作。
例子:
例子 1
国际 en_US 格式:
- <?php
- $number = 1234.56;
- setlocale(LC_MONETARY, "en_US");
- echo money_format("The price is %i", $number);
- ?>
输出:
- The price is USD 1,234.56
例子 2
负数,带有 () 指示负数的 US 国际格式,右侧精度为 2,"*" 为填充字符:
- <?php
- $number = -1234.5672;
- echo money_format("%=*(#10.2n", $number);
- ?>
输出:
- ($********1,234.57)
- 28、nl_langinfo() 函数返回指定的本地信息。
如果成功,则返回指定的本地信息。如果失败,则返回 false。
语法:nl_langinfo(element)
element——必需。规定要返回哪个元素。必须是说明中列出的元素之一。
说明:
时间和日历:
- ABDAY_(1-7) - Abbreviated name of the numbered day of the week
- DAY_(1-7) - Name of the numbered day of the week (DAY_1 = Sunday)
- ABMON_(1-12) - Abbreviated name of the numbered month of the year
- MON_(1-12) - Name of the numbered month of the year
- AM_STR - String for Ante meridian
- PM_STR - String for Post meridian
- D_T_FMT - String that can be used as the format string for strftime() to represent time and date
- D_FMT - String that can be used as the format string for strftime() to represent date
- T_FMT - String that can be used as the format string for strftime() to represent time
- T_FMT_AMPM - String that can be used as the format string for strftime() to represent time in 12-hour format with ante/post meridian
- ERA - Alternate era
- ERA_YEAR - Year in alternate era format
- ERA_D_T_FMT - Date and time in alternate era format (string can be used in strftime())
- ERA_D_FMT - Date in alternate era format (string can be used in strftime())
- ERA_T_FMT - Time in alternate era format (string can be used in strftime())
货币类别:
- INT_CURR_SYMBOL - Currency symbol (example: USD)
- CURRENCY_SYMBOL - Currency symbol (example: $)
- CRNCYSTR - Same as CURRENCY_SYMBOL
- MON_DECIMAL_POINT - Monetary decimal point character
- MON_THOUSANDS_SEP - Monetary thousands separator
- POSITIVE_SIGN - Positive value character
- NEGATIVE_SIGN -Negative value character
- MON_GROUPING - Array displaying how monetary numbers are grouped (example: 1 000 000)
- INT_FRAC_DIGITS - International fractional digits
- FRAC_DIGITS - Local fractional digits
- P_CS_PRECEDES - True (1) if currency symbol is placed in front of a positive value, False (0) if it is placed behind
- P_SEP_BY_SPACE - True (1) if there is a spaces between the currency symbol and a positive value, False (0) otherwise
- N_CS_PRECEDES - True (1) if currency symbol is placed in front of a negative value, False (0) if it is placed behind
- N_SEP_BY_SPACE - True (1) if there is a spaces between the currency symbol and a negative value, False (0) otherwise
- P_SIGN_POSN - Formatting setting. Possible return values:
- 0 - Parentheses surround the quantity and currency symbol
- 1 - The sign string is placed in front of the quantity and currency symbol
- 2 - The sign string is placed after the quantity and currency symbol
- 3 - The sign string is placed immediately in front of the currency symbol
- 4 - The sign string is placed immediately after the currency symbol
- N_SIGN_POSN - Formatting setting. Possible return values:
- 0 - Parentheses surround the quantity and currency symbol
- 1 - The sign string is placed in front of the quantity and currency symbol
- 2 - The sign string is placed after the quantity and currency symbol
- 3 - The sign string is placed immediately in front of the currency symbol
- 4 - The sign string is placed immediately after the currency symbol
数字类别:
- DECIMAL_POINT - Decimal point character
- RADIXCHAR - Same as DECIMAL_POINT
- THOUSANDS_SEP - Separator character for thousands
- THOUSEP - Same as THOUSANDS_SEP
- GROUPING - Array displaying how numbers are grouped (example: 1 000 000)
通信类别:
- YESEXPR - Regex string for matching 'yes' input
- NOEXPR - Regex string for matching 'no' input
- YESSTR - Output string for 'yes'
- NOSTR - Output string for 'no'
代码集类别:
- CODESET Return a string with the name of the character encoding.
提示和注释
注释:money_format() 函数无法在 windows 平台上工作。
提示:与返回所有本地格式化信息的 localeconv() 函数不同,nl_langinfo() 返回指定的信息。
29、nl2br() 函数在字符串中的每个新行 (\n) 之前插入 HTML 换行符 (<br />)。
语法:nl2br(string)
- <?php
- echo nl2br("One line.\nAnother line.");
- ?>
输出:
- One line.
- Another line.
HTML 代码:
- One line.<br />
- Another line.
30、number_format() 函数通过千位分组来格式化数字。
语法:number_format(number,decimals,decimalpoint,separator)
number——必需。要格式化的数字。如果未设置其他参数,则数字会被格式化为不带小数点且以逗号 (,) 作为分隔符。
decimals——可选。规定多少个小数。如果设置了该参数,则使用点号 (.) 作为小数点来格式化数字。
decimalpoint——可选。规定用作小数点的字符串。
separator——可选。规定用作千位分隔符的字符串。仅使用该参数的第一个字符。比如 "xyz" 仅输出 "x"。注释:如果设置了该参数,那么所有其他参数都是必需的。
注释:该函数支持一个、两个或四个参数(不是三个)。
例子
- <?php
- echo number_format("1000000");
- echo number_format("1000000",2);
- echo number_format("1000000",2,",",".");
- ?>
输出:
- 1,000,000
- 1,000,000.00
- 1.000.000,00
PHP学习系列(1)——字符串处理函数(4)的更多相关文章
- hive 学习系列四(用户自定义函数)
如果入参是简单的数据类型,直接继承UDF,实现一个或者多个evaluate 方法. 具体流程如下: 1,实现大写字符转换成小写字符的UDF package com.example.hive.udf; ...
- Python学习系列----第四章 函数
4.1 函数定义 函数是python中重要的工具.函数用关键字 def 来定义.def 关键字后跟一个函数的标识符名称,然后跟一对圆括号.圆括号之中可以包括一些变量名,该行以冒号结尾.接下来是一块 ...
- Python学习系列(四)(列表及其函数)
Python学习系列(四)(列表及其函数) Python学习系列(一)(基础入门) Python学习系列(二)(基础知识) Python学习系列(三)(字符串) 一.基本概念 1,列表是什么? ...
- Python学习系列(三)(字符串)
Python学习系列(三)(字符串) Python学习系列(一)(基础入门) Python学习系列(二)(基础知识) 一个月没有更新博客了,最近工作上有点小忙,实在是没有坚持住,丢久又有感觉写的必要了 ...
- SQLServer学习笔记<>sql的范围内查找,sql数据类型,字符串处理函数
sql的范围内查找 (1)between.....and用法 通常情况下我们查找一个在某固定区域内的所有记录,可以采用>=,<=来写sql语句,例如:查找订单价格在1000到2000之间的 ...
- opencv-python教程学习系列4-opencv绘图函数
前言 opencv-python教程学习系列记录学习python-opencv过程的点滴,本文主要介绍opencv绘图函数,坚持学习,共同进步. 系列教程参照OpenCV-Python中文教程: 系统 ...
- Sql server函数的学习2(游标函数、日期函数、字符串操纵函数)
一.游标函数与变量 游标可以处理多行数据,在过程循环中一次访问一行.和基于集合的高效操作相比,这个功能对系统资源的消耗更大. 可以用一个函数和两个全局变量来管理游标操作 1.CURSOR_STATUS ...
- hive 学习系列之七 hive 常用数据清洗函数
1,case when 的利用,清洗诸如评分等的内容,用例如下. case when new.comment_grade = '五星商户' then 50 when new.comment_grade ...
- perl学习之:字符串函数
一.打开.关闭文件 open的返回值用来确定打开文件的操作是否成功,当其成功时返回非零值,失败时返回零,因此可以如下判断: if (open(MYFILE, "myfile" ...
- (C/C++学习)13.C语言字符串处理函数(一)
说明:字符串处理的函数很多,本文将例举经常遇到的一些函数加以说明. 一.字符串的输入输出 头文件:<stdio.h> 1.利用标准输出函数 printf() 来输出,将格式设置为 s% . ...
随机推荐
- 新的疑问(未解决):VC项目的配置,不是都能在Project -- Properties里设置解决的
现象:死活解决不了引入外部库的LPCWSTR参数传递问题.而用VS新建的项目,就没有这个问题. 我怀疑是当初.pro文件产生VC项目文件时候,做了一些设置,但是内容太长了,又很复杂,所以没法核对. 用 ...
- 查找最小的K个元素,使用最大堆。
查找最小的K个元素,使用最大堆,具体代码如下: #define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace st ...
- hdu5126stars
http://acm.hdu.edu.cn/showproblem.php?pid=5126 首先,对于一个询问,用容斥原理可以拆成8个询问,于是询问变成:给定一个四元组$(i,x_i,y_i,z_i ...
- Java 8 中新的 Date 和 Time 类入门详解
这篇文章主要是java8中新的Date和Time API的实战.新的Date和Time类是java开发者社区千呼万唤始出来的.Java8 之前存在的Date类一直都受人诟病,很多人都会选择使用第三方的 ...
- struts接收参数方式
第一种,直接用action的属性接收,是初学者常用的方法. package com.starain.user; public class User{ private String username; ...
- ganglia单播配置
背景: 有时,由于当前网络不支持组播等种种原因,使用gmond默认的配置gmetad不能获取到各个客户端的全部数据,http://x.x.x.x/ganglia页面一个cluster组只能展示一 ...
- SpriteKit游戏开发
http://blog.csdn.net/larrysai/article/category/1663301 http://blog.csdn.net/ping_yun_long/article/de ...
- 给Android程序猿的六个建议
假设你一年前写的代码 , 在如今看来你还感觉写的非常不错 , 那么说明你学习的不够多. 不要在Context中持有静态引用 public class MainActivity extends Loca ...
- 《火球——UML大战需求分析》(第1章 大话UML)——1.4 如何学好UML?
说明: <火球——UML大战需求分析>是我撰写的一本关于需求分析及UML方面的书,我将会在CSDN上为大家分享前面几章的内容,总字数在几万以上,图片有数十张.欢迎你按文章的序号顺序阅读,谢 ...
- 苹果Swift编程语言新手教程【中国版】
Swift代码语言教程:在刚刚过去的WWDC2014大会上,苹果公司新公布了一种编程语言Swift.据悉.Swift语言继承了C语言以及Objective-C的特性,且克服了C语言的兼容性问题.对于广 ...