PHP中的字符串函数说明PHP

时间:2025年09月03日

/

来源:流水知音

/

编辑:本站小编

收藏本文

下载本文

今天小编在这给大家整理了PHP中的字符串函数说明PHP,本文共4篇,我们一起来阅读吧!本文原稿由网友“流水知音”提供。

篇1:PHP中的字符串函数说明PHP

PHP 中的字符串操作功能是比较多的,重要的有以下这些: (1)echo,print,printf,sprintf 前两个函数是输出字符串.字符串中如果有变量名则被替换成其值. 后两个函数类似于C的同名函数. (2)strchr,strlen,strtok,strrchr,strrev,strstr,strtolower, strtoupper,su

PHP中的字符串操作功能是比较多的,重要的有以下这些:

(1)echo,print,printf,sprintf

前两个函数是输出字符串.字符串中如果有变量名则被替换成其值.

后两个函数类似于C的同名函数.

(2)strchr,strlen,strtok,strrchr,strrev,strstr,strtolower,

strtoupper,substr,ucfirst

这些是常用的字符串操作函数,有些和C中的同名函数意义完全一致.

strrev是把一个字符串翻转.

strtolower和strtoupper的意思应该不用解释了.

ucfirst是把字符串的第一个字符变成大写.

substr是返回字符串的一个子串,用法是:substr(字符串,头,长度).

头位置是 从0算起的.如果是负数,则是从尾部向前数的意思.

(3)Chr,Ord

类似于C的同名函数.

(4)explode,implode,join

这些是和数组有关的函数.

explode(字符串,分割符)返回一个将字符串在分割符处分开所产生的数组.

implode(数组,分割符)返回一个将数组各元素之间插上分割符而成的字符串.

join与implode意义相同.

(5)Chop

去掉字符串尾部的空白.

(6)htmlspecialchars

将字符串中的HTML特殊字符换成它们的名字,例如\\“<\\”变成\\“<\\”.

(7)nl2br

在字符串中的每一个回车前面加上\\“

\\”.

(8)AddSlashes,StripSlashes

分别给字符串中需要加上\\“\\\\”才能用于数据库查询的字符加上和去掉\\“\\\\”.

(9)parse_str

将\\“name1=value1&name2=value2&...\\”类型的字符串分析成一些变量.

例如:

parse_str(\\“a=1&b=2\\”);

生成$a与$b两个变量,值分别为1,2.

如果有两对名字/值的名字部分相同,则后一个的值覆盖前一个的.

如果这两对的名字尾部都有\\“[]\\”,例如\\“a[]=1&a[]=2\\”,则生成数组$a,两个元素分别为1,2

原文转自:www.ltesting.net

篇2:python中实现php的vardump函数功能

这篇文章主要介绍了python中实现php的var_dump函数功能,var_dump函数在PHP中调试时非常实用,本文介绍在Python中实现这个函数,需要的朋友可以参考下

最近在做python的web开发(原谅我的多变,好东西总想都学着,。。node.js也是),不过过程中总遇到些问题,不管是web.py还是django,开发起来确实没用php方便,毕竟存在的时间比较短,很多不完善的地方。

比如我在调试php中最常用的函数,var_dump,在python里找不到合适的替代函数。php中var_dump是一个特别有用的函数,它可以输出任何变量的值,不管你是一个对象还是一个数组,或者只是一个数。它总能用友好的方式输出,我调试的时候经常会需要看某位置的变量信息,调用它就很方便:

但是开发python的时候就没有太好的替代方案。

之前想到repr,但这个函数只是调用了对象中的__str__,和直接print obj没啥区别。print是打印它,而repr是将其作为值返回。如果对象所属的类没有定义__str__这个函数,那么返回的就会是难看的一串字符。

后来又想到了vars 函数,vars函数是python的内建函数,专门用来输出一个对象的内部信息。但这个对象所属的类中必须有__dict__函数。一般的类都有这个dict,但像[]和{}等对象就不存在这个dict,这样调用vars函数就会抛出一个异常:

代码如下:

Traceback (most recent call last):

File “”, line 1, in

TypeError: vars argument must have __dict__ attribute

所以后来几经寻找,找到一个个比较好,功能能够与var_dump类似的函数如下:

代码如下:

def dump(obj):

‘‘‘return a printable representation of an object for debugging‘‘‘

newobj=obj

if ‘__dict__‘ in dir(obj):

newobj=obj.__dict__

if ‘ object at ‘ in str(obj) and not newobj.has_key(‘__type__‘):

newobj[‘__type__‘]=str(obj)

for attr in newobj:

newobj[attr]=dump(newobj[attr])

return newobj

这是使用方式:

代码如下:

class stdClass(object): pass

bj=stdClass()

obj.int=1

obj.tup=(1,2,3,4)

obj.dict={‘a‘:1,‘b‘:2, ‘c‘:3, ‘more‘:{‘z‘:26,‘y‘:25}}

obj.list=[1,2,3,‘a‘,‘b‘,‘c‘,[1,2,3,4]]

obj.subObj=stdClass()

obj.subObj.value=‘foobar‘

from pprint import pprint

pprint(dump(obj))

最后输出是:

代码如下:

{‘__type__‘: ‘<__main__.stdClass object at 0x2b126000b890>‘,

‘dict‘: {‘a‘: 1, ‘c‘: 3, ‘b‘: 2, ‘more‘: {‘y‘: 25, ‘z‘: 26}},

‘int‘: 1,

‘list‘: [1, 2, 3, ‘a‘, ‘b‘, ‘c‘, [1, 2, 3, 4]],

‘subObj‘: {‘__type__‘: ‘<__main__.stdClass object at 0x2b126000b8d0>‘,

‘value‘: ‘foobar‘},

‘tup‘: (1, 2, 3, 4)}

然后github有个开源的module,可以参考:github.com/sha256/python-var-dump

说一下pprint这个函数,他是一个人性化输出的函数,会将要输出的内容用程序员喜欢的方式输出在屏幕上,

参阅这篇文章比较好理解:www.jb51.net/article/60143.htm

篇3:IIS中php sendmail函数无法发送邮件Windows服务器操作系统

在iis+php环境中我们发送邮件一般使用sendmail函数发送邮件,但我配置的环境利用sendmail函数发不了邮件了,后找了很久找到了解决办法,下面我一路给各位同学讲来,

首先你需要先到从glob.com.au/sendmail/下载sendmail.zip文件,点此可以直接下载噢,然后把它解压到如D:/php/sendmail/目录下。

然后打开php.ini文件,找到下面这段代码

代码如下复制代码

[mail function]

; For Win32 only.

SMTP = localhost

smtp_port = 25

; For Win32 only.

;sendmail_from = me@example.com

; For Unix only. You may supply arguments as well (default: “sendmail -t -i”).

; sendmail_path = “”

; Force the addition of the specified parameters to be passed as extra parameters

; to the sendmail binary. These parameters will always replace the value of

; the 5th parameter to mail(), even in safe mode.

;mail.force_extra_parameters =

默认情况下是以本机做为邮件服务器,这里我们需要借用sendmail来发送邮件,用sendmail来配置如用qq、163的邮箱来发送(一般都是以这种方式)所以我们需要把所有的选项都注销,即把SMTP = localhost和smtp_port = 25前面加上“;”然后把sendmai_path=“”前面的“;”删掉,改为sendmai_path=“d:/php/sendmail/sendmail.exe -t”,改完后的即是

代码如下复制代码

[mail function]

; For Win32 only.

;SMTP = localhost

;smtp_port = 25

; For Win32 only.

;sendmail_from = me@example.com

; For Unix only. You may supply arguments as well (default: “sendmail -t -i”).

sendmail_path = “d:/php/sendmail/sendmail.exe -t”

; Force the addition of the specified parameters to be passed as extra parameters

; to the sendmail binary. These parameters will always replace the value of

; the 5th parameter to mail(), even in safe mode.

;mail.force_extra_parameters =

注意以上只需要开启sendmail_path即可,然后保存

接着修改sendmail目录下的sendmail.ini文件,主要填的内容有以下几项

代码如下复制代码

smtp_server=smtp服务器地址(如 smtp.ym.163.com)

auth_username=邮箱登录名(如 info@xxxx.com)

auth_password=邮箱密码(如 xxxxxx)

force_sender=发件人地址全写(如 info@xxxx.com)

另外还有一项

代码如下复制代码

; auto = use SSL for port 465, otherwise try to use TLS

把前面的“;”删除,即开启SSL安全登录选项即可

以上四项正确填写修改完成后保存,然后重启IIS即可正常使用,现在很高兴地测试我的wordpress博客了

现在开始调试wordpress博客,但发现服务器无法发送邮件,sendmail已经安装,但是继续提示邮件发送不成功,

分析了很久,总算找到了原因

第一步:安装sendmail服务

下载sendmail.RAR存放至php目录下的sendmail目录,结构如下:

第二步、配置php.ini文件

代码如下复制代码sendmail_path =”D:/php/sendmail/sendmail.exe -t”

盘符和位置根据sendmail.exe文件位置确定

第三步、配置sendemail.ini

代码如下复制代码

smtp_server=smtp.sina.com.cn

smtp_port=25

这俩个是要求验证的时候的账号,密码

auth_username=ifbs

auth_password=XXXX

第四步、给予cmd.exe权限

C:/WINDOWS/system32/cmd.exe 文件以 users的读权限。不给予权限会出现以下错误。

Warning: mail() [function.mail]: Could not execute mail delivery program

第五步、建立mail.php文件测试结果

代码如下复制代码

$mail = “xxxx@sina.com.cn”;

$subject = “Mail Test”;

$text = “This is a test mail for function mail()”;

if(mail($mail,$subject,$text)){

echo “email send success!”;

}else{

echo “email send fail!”;

}

?>

访问mail.php即可测试结果

篇4:自己写的php中文截取函数mbstrlen和mbsubstr

这篇文章主要介绍了自己写的php中文截取函数mb_strlen和mb_substr,在服务器没mbstring库时可以使用本文函数代替,需要的朋友可以参考下

众所周知,php 自带的 strlen 与 substr 函数没法处理中文字符,于是,我们会用 mb_ 系列函数替代,但是,没有 mbstring 库怎么办?这就需要我们自己写一个来替代了,废话不多说,先上代码:

代码如下:

if ( !function_exists(‘mb_strlen‘) ) {

function mb_strlen ($text, $encode) {

if ($encode==‘UTF-8‘) {

return preg_match_all(‘%(?:

[\\x09\\x0A\\x0D\\x20-\\x7E]          # ASCII

| [\\xC2-\\xDF][\\x80-\\xBF]           # non-overlong 2-byte

| \\xE0[\\xA0-\\xBF][\\x80-\\xBF]      # excluding overlongs

| [\\xE1-\\xEC\\xEE\\xEF][\\x80-\\xBF]{2} # straight 3-byte

| \\xED[\\x80-\\x9F][\\x80-\\xBF]      # excluding surrogates

| \\xF0[\\x90-\\xBF][\\x80-\\xBF]{2}   # planes 1-3

| [\\xF1-\\xF3][\\x80-\\xBF]{3}        # planes 4-15

| \\xF4[\\x80-\\x8F][\\x80-\\xBF]{2}   # plane 16

)%xs‘,$text,$out);

}else{

return strlen($text);

}

}

}

/* from Internet, author unknown */

if (!function_exists(‘mb_substr‘)) {

function mb_substr($str, $start, $len = ‘‘, $encoding=“UTF-8”){

$limit = strlen($str);

for ($s = 0; $start >0;--$start) {// found the real start

if ($s >= $limit)

break;

if ($str[$s] <= “\\x7F”)

++$s;

else {

++$s; // skip length

while ($str[$s] >= “\\x80” && $str[$s] <= “\\xBF”)

++$s;

}

}

if ($len == ‘‘)

return substr($str, $s);

else

for ($e = $s; $len >0; --$len) {//found the real end

if ($e >= $limit)

break;

if ($str[$e] <= “\\x7F”)

++$e;

else {

++$e;//skip length

while ($str[$e] >= “\\x80” && $str[$e] <= “\\xBF” && $e < $limit)

++$e;

}

}

return substr($str, $s, $e - $s);

}

}

php面试题

PHP开发工程师的基本职责说明

php应聘个人简历

PHP聊天室技术

经典PHP笔试题

下载PHP中的字符串函数说明PHP(共4篇)
PHP中的字符串函数说明PHP.doc
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档
点击下载本文文档