Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 怎样用正则匹配不带链接的文字?

怎样用正则匹配不带链接的文字?

Jun 23, 2016 pm 01:28 PM

RegEx.Pattern = "(>[^> 这是ASP版本的 PHP用不行

做站内链接功能
需要循环匹配不带链接的文字
比如

我爱CSDN 我爱CSDN,CSDN不爱我

然后中间有 CSDN的 就替换成

CSDN

可能循环替换多次,如果直接替换 就会乱码了

所以求个正则,如果发现是链接和链接内的文字 就不替换

要求是  已替换过的 不再替换 比如  先把 “CSDN不爱我”  替换为链接 然后 再把 剩下的 “CSDN” 替换成链接
把 “CSDN不爱我” 替换了 ,那这句里的 “CSDN” 也不会再被替换


回复讨论(解决方案)

有大神写过给个例子吗?

不清楚,同求
我爱CSDN 我爱CSDN,CSDN不爱我
然后中间有 CSDN的 就替换成
CSDN
如果第二次编辑的时候, href中的csdn不要替换,

没人会吗?急呐

$s =<<< TXT我爱CSDN 我爱CSDN,CSDN不爱我TXT;$p = '/((?:^|>).*?)(CSDN)([^<]*(?:<(?!\/a)|$))/is';$r = '$1<a href="http://bbs.csdn.net/" title="$2">$2</a>$3';do {  $t = $s;  $s = preg_replace($p, $r, $t);}while($s != $t);echo $s;
Copy after login
Copy after login
我爱CSDN 我爱CSDNCSDN不爱我

$s =<<< TXT我爱CSDN 我爱CSDN,CSDN不爱我TXT;$p = '/((?:^|>).*?)(CSDN)([^<]*(?:<(?!\/a)|$))/is';$r = '$1<a href="http://bbs.csdn.net/" title="$2">$2</a>$3';do {  $t = $s;  $s = preg_replace($p, $r, $t);}while($s != $t);echo $s;
Copy after login
Copy after login
我爱CSDN 我爱CSDNCSDN不爱我



如果重复次数多了 还是会乱掉

比如 继续替换 我爱 爱我 加上链接
CS加链接
S加链接

我现在用的是笨方法
1、把字符串里的图给换成数组标记
2、把链接也换
3、替换
4、还原图的数组标记
5、还原链接的数组标记

这样就不会乱了 但感觉效率好低啊 版主这个能否再改进下呢?

字符串是

文章我是文章14的内容 111 abc 文章333 444 文章asdf; jk555

需要依次给
我是文章 文章
都加上链接http://csdn.net 有没办法呢


	$abc = '欢迎';		$c='liw5ucms PHP版本即将发布,欢迎大家关注!en	<img src="/static/imghw/default1.png"  data-src="/images/hh/02.gif"  class="lazy"  alt="人民"  />dfds	<img src="/static/imghw/default1.png"  data-src="/img/ad.gif"  class="lazy"  alt=""  />dhh呵呵	<a tittle="人民" href="http://www.qiusongsong.com">站长导航</a>	啊哈哈<a href="http://www.5ucms.com">5ucms</a>啊哈哈	<a href="http://qiusongsong.net">邱嵩松</a>tjgad';		echo '原文=<br>'.$c.'<hr>';		echo '替换词=<br>'.$abc.'<hr>';							// 第一步获取字符串里所有图片的正则表达式						preg_match_all("/<img (.*?)\ alt="怎样用正则匹配不带链接的文字?" >/i",$c,$arryimg);						$imgtimes = count($arryimg);						for($j=0;$j<$imgtimes;$j++){ //读取出符合的关键词名称							$img = $arryimg[0][$j]; 							$c = str_ireplace($img,'@img['.$j.']@',$c);//替换掉 变成链接							$qimg[$j] = $img;//存入数组													} 						// 第二步获取字符串里所有超链接的正则表达式						preg_match_all("/<a(.*?)>(.*?)<\/a>/i",$c,$arrya);						$atimes = count($arrya);						for($j=0;$j<$atimes;$j++){ //读取出符合的关键词名称							$a = $arrya[0][$j]; 							$c = str_ireplace($a,'@a['.$j.']@',$c);//替换掉 变成链接							$qa[$j] = $a;//存入数组						}												echo '去图+链接=<br>'.$c.'<hr>';						// 第三步 按指定次数替换						$c = str_replace($abc,'<a href="http://baidu.com" title="百度" >'.$abc.'</a>',$c);//替换掉 指定次数 变成链接												echo '替换结果=<br>'.$c.'<hr>';												// 第四步 恢复旧的图片						for($i=0;$i<count($qimg);$i++){ 							$c = str_replace('@img['.$i.']@',$qimg[$i],$c);//替换掉 指定次数 变成链接						} 						//第五步 恢复旧的链接						for($i=0;$i<count($qa);$i++){ 							$c = str_replace('@a['.$i.']@',$qa[$i],$c);//替换掉 指定次数 变成链接						}  	echo '还原结果=<br>'.$c;
Copy after login

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Discover File Downloads in Laravel with Storage::download Discover File Downloads in Laravel with Storage::download Mar 06, 2025 am 02:22 AM

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

How to Register and Use Laravel Service Providers How to Register and Use Laravel Service Providers Mar 07, 2025 am 01:18 AM

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

See all articles