Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用于搜索的正则表达式+;替换href=";URL";_Php_Regex_Wordpress - Fatal编程技术网

Php 用于搜索的正则表达式+;替换href=";URL";

Php 用于搜索的正则表达式+;替换href=";URL";,php,regex,wordpress,Php,Regex,Wordpress,我对正则表达式一窍不通,也无法用谷歌搜索出一个清晰的解决方案 我想用一个新的url(存储为变量$newurl)搜索并替换锚href中任何url的一些文本($content) 更改此项: <a href="http://blogurl.com/files/foobar.jpg"><img alt="foobar" src="http://blogurl.com/files/2011/03/foobar_thumb.jpg" /></a> 这样做的目的是让Wor

我对正则表达式一窍不通,也无法用谷歌搜索出一个清晰的解决方案

我想用一个新的url(存储为变量$newurl)搜索并替换锚href中任何url的一些文本($content)

更改此项:

<a href="http://blogurl.com/files/foobar.jpg"><img alt="foobar" src="http://blogurl.com/files/2011/03/foobar_thumb.jpg" /></a>
这样做的目的是让WordPress首页上的所有图片链接到他们的帖子,而不是链接到全尺寸的图片(这是他们默认的方式)。通常只有一个url需要替换,但我认为替换所有可能的匹配项不会有什么坏处


希望所有这些都有意义,并提前感谢

这应该可以做到-您可以测试它


(?以下是我想到的要点。希望它能帮助某人:

$content = get_the_content();
$pattern = "/(?<=href=(\"|'))[^\"']+(?=(\"|'))/";
$newurl = get_permalink();
$content = preg_replace($pattern,$newurl,$content);

echo $content;
$content=获取内容();

$pattern=“/(?奖励点:我如何修改此项以仅返回第一个匹配项?脚本不错。但是我如何用不同的值逐个替换大HTML页面中的链接?
preg_replace('Look for href="any-url"', 
'href="$newurl"',$content);
(?<=href=("|'))[^"']+(?=("|'))
$content = get_the_content();
$pattern = "/(?<=href=(\"|'))[^\"']+(?=(\"|'))/";
$newurl = get_permalink();
$content = preg_replace($pattern,$newurl,$content);

echo $content;