php中常用字符串处理代码片段整理


Posted in PHP onNovember 07, 2011

移除 HTML 标签

$text = strip_tags($input, "");

上面的函数主要是使用了strip_tags,具体的使用说明参考。

返回 $start 和 $end 之间的文本

function GetBetween($content,$start,$end){ 
$r = explode($start, $content); 
if (isset($r[1])){ 
$r = explode($end, $r[1]); 
return $r[0]; 
} 
return ''; 
}

将url转换成链接

$url = "Jean-Baptiste Jung (https://3water.com)"; 
$url = preg_replace("#http://([A-z0-9./-]+)#", '<a href="http://www.catswhocode.com/blog/$1" style="font-size: 12px; vertical-align: baseline; background-color: transparent; margin: 0px; padding: 0px; color: #3777af; text-decoration: none; font-weight: bold">$0</a>', $url);

切分字符串为140个字符

function split_to_chunks($to,$text){ 
$total_length = (140 - strlen($to)); 
$text_arr = explode(" ",$text); 
$i=0; 
$message[0]=""; 
foreach ($text_arr as $word){ 
if ( strlen($message[$i] . $word . ' ') <= $total_length ){ 
if ($text_arr[count($text_arr)-1] == $word){ 
$message[$i] .= $word; 
} else { 
$message[$i] .= $word . ' '; 
} 
} else { 
$i++; 
if ($text_arr[count($text_arr)-1] == $word){ 
$message[$i] = $word; 
} else { 
$message[$i] = $word . ' '; 
} 
} 
} 
return $message; 
}

删除字符串中的URL

$string = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $string);

将字符串转成SEO友好的字符串

function slug($str){ 
$str = strtolower(trim($str)); 
$str = preg_replace('/[^a-z0-9-]/', '-', $str); 
$str = preg_replace('/-+/', "-", $str); 
return $str; 
}

解析 CSV 文件

$fh = fopen("contacts.csv", "r"); 
while($line = fgetcsv($fh, 1000, ",")) { 
echo "Contact: {$line[1]}"; 
}

字符串搜索

function contains($str, $content, $ignorecase=true){ 
if ($ignorecase){ 
$str = strtolower($str); 
$content = strtolower($content); 
} 
return strpos($content,$str) ? true : false; 
}

检查字符串是否以某个串开始

function String_Begins_With($needle, $haystack { 
return (substr($haystack, 0, strlen($needle))==$needle); 
}

从字符串中提取email地址

function extract_emails($str){ 
// This regular expression extracts all emails from a string: 
$regexp = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i'; 
preg_match_all($regexp, $str, $m); return isset($m[0]) ? $m[0] : array(); 
} 
$test_string = 'This is a test string... 
test1@example.org 
Test different formats: 
test2@example.org; 
<a href="test3@example.org">foobar</a> 
<test4@example.org> 
strange formats: 
test5@example.org 
test6[at]example.org 
test7@example.net.org.com 
test8@ example.org 
test9@!foo!.org 
foobar 
'; 
print_r(extract_emails($test_string));
PHP 相关文章推荐
web方式ftp
Oct 09 PHP
mysql_fetch_row,mysql_fetch_array,mysql_fetch_assoc的区别
Apr 24 PHP
php 操作调试的方法
Jul 12 PHP
php随机输出名人名言的代码
Oct 07 PHP
php安全开发 添加随机字符串验证,防止伪造跨站请求
Feb 14 PHP
深入PHP FTP类的详解
Jun 13 PHP
PHP @ at 记号的作用示例介绍
Oct 10 PHP
PHP中使用file_get_contents抓取网页中文乱码问题解决方法
Dec 17 PHP
Yii2创建表单(ActiveForm)方法详解
Jul 23 PHP
ThinkPHP5实现作业管理系统中处理学生未交作业与已交作业信息的方法
Nov 12 PHP
PHP第三方登录―QQ登录实现方法
Feb 06 PHP
Laravel 手动开关 Eloquent 修改器的操作方法
Dec 30 PHP
php smarty截取中文字符乱码问题?gb2312/utf-8
Nov 07 #PHP
PHP面向对象概念
Nov 06 #PHP
php 记录进行累加并显示总时长为秒的结果
Nov 04 #PHP
php 按指定元素值去除数组元素的实现方法
Nov 04 #PHP
php数组函数序列之array_search()- 按元素值返回键名
Nov 04 #PHP
php 伪造本地文件包含漏洞的代码
Nov 03 #PHP
有关php运算符的知识大全
Nov 03 #PHP
You might like
php安全之直接用$获取值而不$_GET 字符转义
2012/06/03 PHP
PHP图片处理之使用imagecopyresampled函数实现图片缩放例子
2014/11/19 PHP
php为字符串前后添加指定数量字符的方法
2015/05/04 PHP
jQuery EasyUI API 中文文档 - ComboTree组合树
2011/10/11 Javascript
js window.open弹出新的网页窗口
2014/01/16 Javascript
JS获取单击按钮单元格所在行的信息
2014/06/17 Javascript
深入理解JavaScript系列(17):面向对象编程之概论详细介绍
2015/03/04 Javascript
解决拦截器对ajax请求的拦截实例详解
2016/12/21 Javascript
Bootstrap modal 多弹窗之叠加引起的滚动条遮罩阴影问题
2017/02/27 Javascript
关于使用js算总价的问题
2017/06/23 Javascript
p5.js 毕达哥拉斯树的实现代码
2018/03/23 Javascript
vue删除html内容的标签样式实例
2018/09/13 Javascript
[01:11:15]VGJ.S vs Secret 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
python判断windows系统是32位还是64位的方法
2015/05/11 Python
Python 常用的安装Module方式汇总
2017/05/06 Python
Python 读取指定文件夹下的所有图像方法
2018/04/27 Python
python函数与方法的区别总结
2019/06/23 Python
Python中logging日志库实例详解
2020/02/19 Python
VScode连接远程服务器上的jupyter notebook的实现
2020/04/23 Python
Python 使用SFTP和FTP实现对服务器的文件下载功能
2020/12/17 Python
HTML5探秘:用requestAnimationFrame优化Web动画
2018/06/03 HTML / CSS
html5的canvas实现3d雪花飘舞效果
2013/12/27 HTML / CSS
HTML5实现简单图片上传所遇到的问题及解决办法
2016/01/20 HTML / CSS
巴西Mr. Cat在线商店:购买包包和鞋子
2019/09/08 全球购物
Whistles官网:英国女装品牌
2020/08/14 全球购物
加拿大拼图大师:Puzzle Master
2020/12/28 全球购物
Python如何实现单例模式
2016/06/03 面试题
什么是servlet
2012/05/08 面试题
急诊科护士自我鉴定
2013/10/14 职场文书
工厂保洁员岗位职责
2013/12/04 职场文书
法学院毕业生求职信
2014/06/25 职场文书
2015年药房工作总结
2015/04/25 职场文书
贫民窟的百万富翁观后感
2015/06/09 职场文书
中学后勤工作总结2015
2015/07/22 职场文书
驾驶员管理制度范本
2015/08/06 职场文书
婚宴祝酒词大全
2015/08/10 职场文书