Home > php教程 > PHP源码 > PHP正则匹配反斜杠''和美元'$'

PHP正则匹配反斜杠''和美元'$'

PHP中文网
Release: 2016-05-25 17:06:24
Original
1030 people have browsed it

$content = '11111112222222<\/td>3$'; 

//' \\\\\/ ' 第1个'\'转义字符串的第2个'\',字符串为'\' 
//第3个'\'转义第4个'\',相当于 字符串 '\' 
//第5个'\'转义第4个'/',相当于 字符串 '/' 
//字符合起来为'\\/' 两个'\\' 正则表达式看做'\' 
$pattern = '/([0-9]{7,})<\\\\\/td>\d\\$$/'; 

上面方法out了,使用\Q \E,具体东西看评论

1.test.php

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

2.php代码

$content = &#39;1111111<td>2222222<\/td>3$&#39;; 
$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");
Copy after login

3.output.txt

Array
(
    [0] => Array
        (
            [0] => <td>2222222<\/td>3$
        )
 
    [1] => Array
        (
            [0] => 2222222
        )
 
)
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template