PHP处理字符串的能力非常强大,方法也是多种多样,但有的时候你需要选择一种最简单且理想的解决方法。文章列举了10个PHP中常见的字符串处理案例,并提供了相对应的最理想的处理方法。
1.确定一个字符串的长度
这是文章中最明显的一个例子,其中的问题是我们如何来确定一个字符串的长度,这里我们不能不提的就是strlen()函数:
$text = "sunny day"; $count = strlen($text); // $count = 9
2.截取文本,创建一个摘要
新闻性质的网站通常会截取一个大约200字左右的段落,并在次段落的末尾加上省略号来形成一个摘要,这时,你可以使用substr_replace()函数来实现此功能。由于篇幅的原因,这里只演示对40个字符的限制:
$article = "BREAKING NEWS: In ultimate irony, man bites dog."; $summary = substr_replace($article, "…", 40); // $summary = "BREAKING NEWS: In ultimate irony, man bi…"
3.计算字符串中的字符和单词数
相信您经常会看到一些博客或者新闻类文章,来总结文章的总字数,或者我们也经常看到一些投稿的要求:在一定的字数范围内。这时,你可以使用str_word_count()函数来计算文章字数的总和:
$article = "BREAKING NEWS: In ultimate irony, man bites dog."; $wordCount = str_word_count($article); // $wordCount = 8
有的时候你需要更加严格的控制投稿者的使用空间,例如一些批注等等。如果你想知道有多少个字符来组成一个数组,请使用count_chars()函数。
4.解析CSV文件
数据通常是以逗号分隔的形式存储在文件中的(如一个已知的CSV文件),CSV文件使用一个逗号或者类似于预定义符号,将每列字符串组成一个单独的行。你可能经常创建PHP脚本来导入这些数据,或者解析出你所需要的东西,这些年来,我也看到过很多解析CSV文件的方法,最常见的就是使用fgets()和explode()函数的组合来读取和解析文件,然而,最简单的方法是使用一个函数来解决问题,但它并不属于PHP的字符串处理库里的一部分:fgetcsv()函数。使用fopen()和fgetcsv()函数,我们能够很容易的解析这个文件,同时检索出每个联系人的名字:
$fh = fopen("contacts.csv", "r"); while($line = fgetcsv($fh, 1000, ",")) { echo "Contact: {$line[1]}"; }
5.转换成一个字符串数组
某些时候,你可能需要创建CSV文件,同时又在这些文件中进行读取,这就意味着你需要将那些同逗号分隔的字符串转换成数据。如果这些数据最初是从数据库检索出的,那么它很可能会只给您提供一个数组。这时,您可以使用implode()函数,将这些字符串转换成一个数组:
$csv = implode(",", $record);
6.将网址转换成超链接
目前许多WYSIWYG编辑器提供的工具栏,都允许用户标记文本,包括超链接。但是,当内容呈现到页面上时,你可以很容易的自动执行此过程,同时保证您不出现额外的错误。要转换成超链接的URL,你可以使用preg_replace()函数,它可以按照正则表达式来搜索一个字符串,并定义了URL的结构:
$url = "W.J. Gilmore, LLC (http://www.veryhuo.com)"; $url = preg_replace("/http://([A-z0-9./-]+)/", "$0", $url); // $url = "W.J. Gilmore, LLC (http://www.veryhuo.com)"
7.从一个字符串中去除HTML标签
作为Web开发人员,其中的一个主要工作就是要确保用户输入中不含有危险字符,如果有,这会导致SQL注入或脚本攻击。PHP语言中包含了很多安全方面的功能,这些功能能够帮助你过滤数据,包括延长过滤器。例如,你可以允许用户中带有一些基本的HTML语句,包括一些注释。实现这个功能,你可以使用带有检查功能函数:strip_tags()。它在默认的情况下是从字符串中删除所有的HTML标签,但同时也允许覆盖默认或者你指定的标签。例如,在下面的例子中,你可以除去所有的标签:
$text = strip_tags($input, " ");
8.比较两个字符串
比较两个字符串,以确保它们是相同的。例如,判断用户第一次与第二次输入的密码是否相同,你可以使用substr_compare()函数来很容易的现实:
$pswd = "secret"; $pswd2 = "secret"; if (! strcmp($pswd, $pswd2)) { echo "The passwords are not identical!"; }
如果你想判断两个字符串不区分大小写,可以使用strcasecmp()函数。
9.转换换行符
在本文中我介绍了如何轻松转换成超超链接的URL,现在介绍nl2br()函数,这个函数能够帮助你将任何换行符转换成HTML标签。
$comment = nl2br($comment);
10.应用自动换行
应用自动换行,你可以使用PHP中的这个函数:wordwrap():
$speech = "Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal."; echo wordwrap($speech, 30);
执行上面的代码,结果是:
Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.
原文地址:http://phpbuilder.com/columns/Jason_Gilmore060210.php3
原文名:10 Easy Solutions for PHP String Manipulation
作者:W. Jason Gilmore
波比源码 » PHP中常见的10个字符串处理实例代码
order levaquin 500mg online cheap purchase levofloxacin pills
order avodart 0.5mg online celecoxib 200mg over the counter order generic ondansetron
spironolactone brand finasteride 5mg canada fluconazole buy online
acillin uk erythromycin 250mg pill buy erythromycin 250mg
sildenafil 100mg uk bimatoprost online buy methocarbamol order
sildenafil for sale suhagra ca buy estrace 2mg for sale
order lamictal 200mg for sale purchase lamictal generic buy retin gel online
tadalis sale cheap tadalafil 20mg voltaren 50mg ca
order generic accutane 40mg oral zithromax 250mg order azithromycin for sale
indomethacin canada trimox online order purchase amoxicillin pills
cialis 5mg kaufen für männer tadalafil 5mg für männer sildenafil 50mg kaufen für männer
isotretinoin drug buy azithromycin without prescription ivermectin 2mg
provigil 200mg for sale buy provigil 200mg for sale acetazolamide 250mg drug
buy doxycycline 100mg for sale order doxycycline 100mg pills buy lasix 100mg generic
buspirone 5mg brand buy amiodarone 100mg without prescription order ditropan 2.5mg online
order hytrin 5mg sale leflunomide 10mg us azulfidine for sale
benicar 10mg us olmesartan 20mg brand buy diamox 250 mg generic
tacrolimus 1mg usa order labetalol 100 mg pill ursodiol brand
purchase imdur generic order azathioprine 25mg generic micardis 80mg price
order generic imuran protonix 20mg over the counter order viagra pill
avlosulfon pills nifedipine 10mg tablet perindopril buy online
oral provera 5mg order hydrochlorothiazide sale purchase periactin pills
order luvox 100mg pill purchase luvox online glucotrol 10mg pills
isotretinoin pill order amoxil pills deltasone 20mg for sale
nootropil 800 mg us order nootropil generic buy generic sildenafil 50mg
azithromycin 250mg cheap neurontin 800mg brand buy neurontin 800mg
order itraconazole online cheap buy itraconazole 100 mg pills brand tinidazole
generic clozapine 50mg buy decadron 0,0,5 mg generic dexamethasone sale
zyvox 600 mg cost online slots for real money casino games online real money
purchase prilosec generic real money poker online slot casino
lopressor 50mg brand metoprolol 50mg pills vardenafil 10mg pills
buy clomid 50mg generic ventolin 2mg over the counter real money online casino
oral cialis 40mg order tadalafil 40mg online generic sildenafil 100mg
sumatriptan us avodart 0.5mg us purchase avodart pill
buy ranitidine 150mg generic ranitidine 300mg without prescription brand celecoxib
blackjack online for real money slots real money online casino no deposit bonus
flomax 0.4mg uk buy spironolactone 25mg generic spironolactone 100mg pills
flagyl over the counter bactrim 960mg price bactrim pills
order cephalexin 500mg for sale order cephalexin 250mg generic erythromycin generic
cialis 5mg cost order tadalafil 20mg sale best viagra sites online
purchase sildalis for sale order generic estradiol lamictal oral
free blackjack games roulette casino order generic modafinil 100mg
oral sildenafil 50mg buy sildenafil pills buy rhinocort generic