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

yipeiwu_com6年前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程序设计有所帮助。

相关文章

php使用number_format函数截取小数的方法分析

本文实例讲述了php使用number_format函数截取小数的方法。分享给大家供大家参考,具体如下: 大家知道用php的number_format()函数可以将数字按千分组. 但是它会...

php错误日志简单配置方法

本文实例讲述了php配置错误日志的方法。分享给大家供大家参考,具体如下: php.ini: ; 错误日志 log_errors = On ; 显示错误 display_errors...

PHP设计模式之装饰器模式定义与用法详解

本文实例讲述了PHP设计模式之装饰器模式定义与用法。分享给大家供大家参考,具体如下: 什么是装饰器模式 作为一种结构型模式, 装饰器(Decorator)模式就是对一个已有结构增加"装饰...

curl实现站外采集的方法和技巧

选择curl的理由 关于curl与file_get_contents,摘抄一段通俗易懂的对比:file_get_contents其实是一堆内置的文件操作函数的合并版本,比如file_ex...

使用PHP获取汉字的拼音(全部与首字母)

废话不多说,直接上代码:复制代码 代码如下:<?php class GetPingYing {     private $pylist = array...