Home> 类库下载> PHP类库> body text

Summary of php regular expression examples

高洛峰
Release: 2016-10-20 15:14:53
Original
1350 people have browsed it

1. 根据购物车中的商品Item ID,准确判断客人的原装机型号。

"; print_r($arr); echo "
Copy after login
"; } ?>


2.匹配日期

\n"; ?>
Copy after login


3. 搜索单词 web

Copy after login


4. 从url 中取出域名

Copy after login

5. 你知道下面的程序输出什么?

]+>(.*)]+>|U", "example: 
this is a test
", $out, PREG_PATTERN_ORDER); // 注意PREG_PATTERN_ORDER和PREG_SET_ORDER的区别 print $out[0][0].", ".$out[0][1]."\n"; print $out[1][0].", ".$out[1][1]."\n"; echo "
"; print_r($out); ?>
Copy after login

6. 功能: 转义正则表达式字符,此函数很有用,可以供你写正则表达式时参考

Copy after login

7. 看人家如何用 preg_quote() 函数实现高亮显示

".$word."",$textbody); echo $textbody; ?>
Copy after login

8. 用回调函数执行正则表达式的搜索和替换

Copy after login



9. 在 preg_replace() 中使用索引数组

Copy after login

10. 将 HTML 转换成文本

]*?>.*?'si", // 去掉 # "'<[\/\!]*?[^<>]*?>'si", // 去掉 HTML 标记 "'([\r\n])[\s]+'", // 去掉空白字符 "'&(quot|#34);'i", // 替换 HTML 实体 "'&(amp|#38);'i", "'&(lt|#60);'i", "'&(gt|#62);'i", "'&(nbsp|#160);'i", "'&(iexcl|#161);'i", "'&(cent|#162);'i", "'&(pound|#163);'i", "'&(copy|#169);'i", "'&#(\d+);'e"); // 作为 PHP 代码运行 $replace = array ("", "", "\\1", "\"", "&", "<", ">", " ", chr(161), chr(162), chr(163), chr(169), "chr(\\1)"); $text = preg_replace ($search, $replace, $document); ?>
Copy after login



Related labels:
php
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
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!