本文章先是讲述了简单的利用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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
