Php preg_replace_回调返回空
首先,我应该说这是一个基于WordPress的问题,我最初是在WordPress StackExchange上问的。但我认为这已经变成了一个更基于PHP的问题,所以这就是我在这里提问的原因 基本上,我编写了这个preg_replace_回调函数,在保存/发布帖子时,它将用WP Uploads目录中的URL替换所有图像URL。我在这方面取得了短暂的成功;有时它是有效的,但只有在我的网站测试示例中的一个URL上,我有3个img标记,每个标记都用段落分开 这是我的密码:Php preg_replace_回调返回空,php,preg-replace,preg-replace-callback,Php,Preg Replace,Preg Replace Callback,首先,我应该说这是一个基于WordPress的问题,我最初是在WordPress StackExchange上问的。但我认为这已经变成了一个更基于PHP的问题,所以这就是我在这里提问的原因 基本上,我编写了这个preg_replace_回调函数,在保存/发布帖子时,它将用WP Uploads目录中的URL替换所有图像URL。我在这方面取得了短暂的成功;有时它是有效的,但只有在我的网站测试示例中的一个URL上,我有3个img标记,每个标记都用段落分开 这是我的密码: add_filter('con
add_filter('content_save_pre', 'getpostimgs'); // content_save_pre filter may be depreceated? => http://adambrown.info/p/wp_hooks/hook/content_save_pre
function getpostimgs() {
global $post;
$postContent = $post->post_content;
$content = preg_replace_callback(
'/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', // pattern to match to (i.e the contents of an <img src="..." /> tag)
function ($match) {
$imgURL = $match[1]; // the second array (#1) (0-based) is the array with the URLs in. First array (#0) has the whole img tag in.
$image_data = file_get_contents($imgURL); // Get image data
$filename = basename($imgURL) . "-" . $post->ID . ".jpg"; // Create image file name
if( wp_mkdir_p( $upload_dir['path'] ) ) { // check upload file exists and the permissions on it are correct
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
} // save file to server
file_put_contents( $file, $image_data ); // save the file to the server
return $file;
},
$postContent
);
return $content;
}
如果我在一个.php页面上调用这个函数,它就会工作,即返回的帖子内容中的原始图像URL已经被上传目录URL替换,因此regex和其他东西是正确的。但是,当我试图发布/更新帖子时,它不起作用,因为内容通常会被删除。有什么想法吗?getpostimgs是一个过滤器,因此接受参数$post\u content。我不确定$post->post\u内容此时是否有效。应该有效,$post在这两个函数中都是全局变量,并且post中的图像是用正确的post ID拾取和保存的,因此$post->post\u内容应该有效。“getpostimgs是一个过滤器,因此接受$post_content”参数是什么意思?你的意思是这已经存在于WP的另一部分中,我应该重命名我的函数吗?过滤器不是动作,它们被调用时至少有一个参数-要操作的数据。我不确定,但是$post的所有成员,特别是post_内容,可能尚未生效,因为他们正在保存/更新-我将阅读代码,看看是否可以给您一个更明确的答案。无论如何,你不需要这个,应该用这个论点来代替。啊,好的,这是有道理的。所以我应该有一个论点,比如将被操纵的原始帖子内容,作为我的论点?感谢您的帮助:过滤器内容_save_pre是从sanitize_post_字段调用的,该字段是从sanitize_post和其他一些地方调用的。可以使用$post对象或$post数组调用sanitize_post。我猜$post数组是$post对象的前身,因此$post对象还无效。这是一个猜测。对不起,我现在没有时间了。
function getpostimgs() {
global $post;
$postContent = $post->post_content;
$content = preg_replace_callback(
'/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', // pattern to match to (i.e the contents of an <img src="..." /> tag)
function ($match) {
global $post, $postContent;
$upload_dir = wp_upload_dir(); // Set upload folder
$imgURL = $match[1];
$image_data = file_get_contents($imgURL); // Get image data
$filename = basename($imgURL) . "-" . $post->ID . ".jpg"; // Create image file name
if( wp_mkdir_p( $upload_dir['path'] ) ) { // check upload file exists and the permissions on it are correct
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
} // save file to server
file_put_contents( $file, $image_data ); // save the file to the server
return $file;
},
$postContent
);
return $content;
}
add_filter('content_save_pre', 'getpostimgs'); // content_save_pre filter may be depreceated? => http://adambrown.info/p/wp_hooks/hook/content_save_pre