Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.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/1/wordpress/12.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 使用preg_replace需要替换映像src中的随机目录_Php_Wordpress_Preg Replace - Fatal编程技术网

Php 使用preg_replace需要替换映像src中的随机目录

Php 使用preg_replace需要替换映像src中的随机目录,php,wordpress,preg-replace,Php,Wordpress,Preg Replace,我已经为此挣扎了两个小时,这让我发疯。我认为这并不难。我正在使用Wordpress,需要将IMG URL从旧路径替换为新路径。问题是..关于url的所有内容都是静态的,除了一个随机的特定目录 例如: b1493cd89a29c0a2d1d8e0939f05d8ee/booth_w640.jpeg 应该成为 /wp content/uploads/imports/booth_w640.jpeg 粗体部分是随机的。所以我在我的wordpress functions.php中有这个 函数替换内容($c

我已经为此挣扎了两个小时,这让我发疯。我认为这并不难。我正在使用Wordpress,需要将IMG URL从旧路径替换为新路径。问题是..关于url的所有内容都是静态的,除了一个随机的特定目录

例如:

b1493cd89a29c0a2d1d8e0939f05d8ee/booth_w640.jpeg

应该成为

/wp content/uploads/imports/booth_w640.jpeg

粗体部分是随机的。所以我在我的wordpress functions.php中有这个

函数替换内容($content){
$reg='1#/https://cdn2.content.mysite.com/uploads/user/76eb326b-62ff-4d37-bf4b-01a428e2f9f6/0ffd6c15-8a13-437c-9661-36edfe11cb41/Image/([^/]+)#i';
$rep='/wp content/uploads/imports';
$content=preg_replace($reg,$rep,$content);
返回$content;
}
添加_过滤器('the_content','replace_content');

但这是行不通的。我想不出来。有什么帮助吗?

我想你需要的是:

function replace_content($content) {
    $reg = '#/static-part-of-url/([^/]+)#i';
    $rep = '/wp-content/uploads/imports';
    $content = preg_replace($reg, $rep ,$content);
    return $content;
}
add_filter('the_content','replace_content');
尝试匹配URL时,使用与
/
不同的分隔符更好


脚本正在运行。

我使用以下代码确定了问题的答案

preg_match( '/src="([^"]*)"/i', $content, $match ) ;
$getURL = $match[1];
$urlArr = explode("/",$getURL);
$fileName = end($urlArr);
$newURL = "/blog/wp-content/uploads/imports/" . $fileName;
$content = str_replace($getURL, $newURL, $content);

[.*]
只会与
*
字面上匹配。那么我就接近了?因为现在,Wordpress将所有内容都替换为零…而不是“仅仅”替换内容中的图像src。只需将
'./https:
更改为
'.\https:
这就是我的代码目前所在的位置。由于图像src仍然没有被正确替换,请使用
变量转储($content)
在函数中检查它是否真的是您认为的那样。现在,内容再次显示。但是图像的URL仍然相同。函数replace_content($content){$reg='#/^/]+)#i'$rep='/wp content/uploads/imports';$content=preg_replace($reg,$rep,$content);返回$content;}添加_过滤器('the_content','replace_content')@BryanKohlmeier你能把URL的最终结果也放在你的问题中吗?替换过滤器后的URL应该是什么?我的意思是。我显然没有在stackoverflow上发布。很抱歉,我的编辑搞砸了。@BryanKohlmeier,在正则表达式替换后它应该变成什么?很抱歉。如果错误的url是,我希望它变成/wp content/uploads/imports/file.jpg