PHP正则匹配反斜杠'\'和美元'$'的方法

yipeiwu_com7年前PHP代码库

本文实例讲述了PHP正则匹配反斜杠'\'和美元'$'的方法。分享给大家供大家参考,具体如下:

1. test.php:

<?php
$content = '1111111<td>2222222<\/td>3$';
//'\\\\\/' 第1个'\'转义字符串的第2个'\',字符串为'\'
//第3个'\'转义第4个'\',相当于字符串'\'
//第5个'\'转义第4个'/',相当于字符串'/'
//字符合起来为'\\/' 两个'\\' 正则表达式看做'\'
$pattern = '/<td>([0-9]{7,})<\\\\\/td>\d\\$$/';
$result = preg_match_all($pattern, $content, $match_result);
if($result)
  print_r($match_result);
else
  echo("not match");

2. 方法二:

$content = '1111111<td>2222222<\/td>3$';
$pattern = "!<td>(\d{7,})<\Q\/\Etd>\d\Q$\E!";
$result = preg_match_all($pattern, $content, $m);
if($result)
  print_r($m);
else
  echo("not match");

3. 运行结果:

Array
(
  [0] => Array
    (
      [0] => <td>2222222<\/td>3$
    )
  [1] => Array
    (
      [0] => 2222222
    )
)

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript

正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php正则表达式用法总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

相关文章

ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单

首先是数据库的设计。分类表叫cate. 我做的是分类数据的二级联动,数据需要的字段有:id,name(中文名),pid(父id). 父id的设置: 若数据没有上一级,则父id为0,若有...

spl_autoload_register与autoload的区别详解

spl_autoload_register(PHP 5 >= 5.1.2)spl_autoload_register — 注册__autoload()函数说明bool spl_au...

为IP查询添加GOOGLE地图功能的代码

1. 在使用google api之前,我们需要先申请一个key,作为唯一的标示加在url后面。 2. google地图api里面有一个可以查询到某个地点的详细信息的地址,例如查询北京的:...

php生成无限栏目树

栏目数组: $arr=Array( Array('cid' => 2,'cname' => '新闻','pid' => 0), Array('cid' =&...

php新浪微博登录接口用法实例

本文实例讲述了php新浪微博登录接口用法。分享给大家供大家参考。具体分析如下: 在做微博登陆之前是需要申请到APP KEY 和App Secret,这个的申请方式请去 open.weib...