源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-11-04 15:47 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:php中常用字符串处理代码片段整理
移除 HTML 标签
[url=http://www.1sucai.cn/w3school/php/func_string_strip_tags.htm]strip_tags[/url],具体的使用说明参考。   返回 $start 和 $end 之间的文本
[url=http://www.catswhocode.com/blog/$1]
  切分字符串为140个字符   删除字符串中的URL
[u]复制代码[/u] 代码如下:
$string = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $string);
  将字符串转成SEO友好的字符串
[u]复制代码[/u] 代码如下:
function slug($str){ $str = strtolower(trim($str)); $str = preg_replace('/[^a-z0-9-]/', '-', $str); $str = preg_replace('/-+/', "-", $str); return $str; }
  解析 CSV 文件
[u]复制代码[/u] 代码如下:
$fh = fopen("contacts.csv", "r"); while($line = fgetcsv($fh, 1000, ",")) { echo "Contact: {$line[1]}"; }
  字符串搜索
[u]复制代码[/u] 代码如下:
function contains($str, $content, $ignorecase=true){ if ($ignorecase){ $str = strtolower($str); $content = strtolower($content); } return strpos($content,$str) ? true : false; }
  检查字符串是否以某个串开始
[u]复制代码[/u] 代码如下:
function String_Begins_With($needle, $haystack { return (substr($haystack, 0, strlen($needle))==$needle); }
  从字符串中提取email地址
[u]复制代码[/u] 代码如下:
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));
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部