本文章先是讲述了简单的利用php正则表达试来替换字符中的一些字符,然后再再告诉你正则表达式替换URL链接地址为指定url地址方法有需要学习的朋友可参考。
PHP正则表达式替换的相关概念:
preg_replace:执行正则表达式的搜索和替换
mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit]) preg_replace:允许你替换字符串中匹配到你定义的正则表达式。一个简单的注释移除功能:
preg_replace('[(/*)+.+(*/)]', '', $val); 这段代码可以移除在 PHP 和 CSS 中使用 /* 注释 */ 格式的多多行注释。其中的三个参数分别为正则表达式,要替换成的字符串和要替换的目标字符串(这里要做移除功能,所以是个空白字符串 -> '')。如果你想匹配次级规则,可以使用 $0 代表全部匹配,$1、$2 等,以此类推代表分别的次级规则。
如把空格替换-
例子 1 :
代码如下 | 复制代码 |
$str = "The quick brown fox jumped over the lazy dog."; The-quick-brown-fox-jumped-over-the-lazy-dog. |
替换数组
代码如下 | 复制代码 |
$str = "The quick brown fox jumped over the lazy dog."; $patterns[0] = "/quick/"; $replacements[2] = "bear"; print preg_replace($patterns, $replacements, $str); |
链接形式是:Bootstrap中文网 ,
而我们采集后希望变成:Bootstrap中文网
这里面涉及到正则出来url链接的关键字问题。示例代码如下:
代码如下 | 复制代码 |
$a = 'Bootstrap中文网提供Bootstrap中文手册,学习Bootstrap入门教程,Bootstrap是Twitter推出的一个用于前端开发的开源工具包。它由Twitter的设计师Mark Otto和Jacob Thornton合作开发,是一个CSS/HTML框架。Bootstrap中文网致力于为广大国内开发者提供详尽的中文文档、代码实例等,助力开发者掌握并使用这一框架。'; $lines_string=preg_replace("(]*>(.+?))","$1",$a); |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use