php - 将 preg_replace 转换为 preg_replace_callback
问题描述
这是我的字符串
$wpheader_output = "<!-- This site is optimized with the Yoast SEO plugin v11.0 - https://yoast.com/wordpress/plugins/seo/ -->
<meta name='msvalidate.01' content='C0C36649CDED53868CDF54EE8754432A' />
<meta name='google-site-verification' content='JhzC8-kGVNcajTnSfsFDtEbpnDt0FcTe6Y3I5z79Lig' />
<link rel='dns-prefetch' href='//s.w.org' />
<style id='yasrcss-inline-css' type='text/css'>
.star-rating {
background-image: url('https://www.example.com/wp-content/plugins/yet-another-stars-rating/img/star_oxy_0.svg');
}
.star-rating .star-value {
background: url('https://www.example.com/wp-content/plugins/yet-another-stars-rating/img/star_oxy_1.svg') ;
}
.star-rating .star-value {
-moz-transform: scaleX(-1);
-o-transform: scaleX(-1);
-webkit-transform: scaleX(-1);
transform: scaleX(-1);
filter: FlipH;
-ms-filter: 'FlipH';
right: 0;
left: auto;
}
</style>
<link rel='stylesheet' id='style-css' href='https://www.example.com/wp-content/themes/newseo/style.css' type='text/css' media='all' />
<link rel='https://api.w.org/' href='https://www.example.com/wp-json/' />
<script type='text/javascript'>
window._wp_rp_static_base_url = 'https://wprp.zemanta.com/static/';
window._wp_rp_wp_ajax_url = 'https://www.example.com/wp-admin/admin-ajax.php';
window._wp_rp_plugin_version = '3.6.4';
window._wp_rp_post_id = '12000';
window._wp_rp_num_rel_posts = '7';
window._wp_rp_thumbnails = false;
window._wp_rp_post_tags = [];
window._wp_rp_promoted_content = true;
window._wp_rp_admin_ajax_url = 'https://www.example.com/wp-admin/admin-ajax.php';
window._wp_rp_plugin_static_base_url = 'https://www.example.com/wp-content/plugins/wordpress-23-related-posts-plugin/static/';
</script>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js'></script> " ;
我需要删除这一行
<!-- This site is optimized with the Yoast SEO plugin v11.0 - https://yoast.com/wordpress/plugins/seo/ -->
我曾经用perg_replace
它
$wpheader_output = preg_replace("/(<!-- This site is optimized with the Yoast SEO) (.|\n)*(plugins\/seo\/ -->)/", " ", $wpheader_output);
但是在升级 php 版本之后,这将不再起作用,所以我尝试了preg_replace_callback
这是我的代码
$wpheader_output = preg_replace_callback("/(<!-- This site is optimized with the Yoast SEO) (.|\n)*(plugins\/seo\/ -->)/" , function($matches) {
return " ";
}, $wpheader_output);
但它会在这个字符串上返回 null
解决方案
推荐阅读
- junit5 - 无法运行测试类“没有使用测试运行器 'Junit5' 找到测试”
- php - CDN 链接无法使用 PHP 包含
- reactjs - 如何严格键入包含要渲染的内部组件和内部组件道具的组件道具?
- kubernetes - kubernetes 入口服务不会在 http 或 https 中转发
- string - 如何在 Markdown Julia 中使用固定宽度的插值字符串格式?
- python - Pandas 在导入 csv 文件时添加了不存在的**尾随**零
- python - tensorflow / keras:不能挤压dim [1],预期尺寸为1,得到3
- python - Python tkinter - 即使使用多线程,GUI 也“滞后”
- python - 如何使用 Python Selenium 与不同格式的标题进行交互?
- lua - lua 序列化和反序列化具有函数值的表