admin管理员组文章数量:1313175
I'm trying to alter the content of an email generated by a plugin, removing a lengthy section that starts with <p> NOTE:
(the space is actually a tab) and ending with /</a><br />
(lines 2-5 below). There are all sorts of characters and line feeds in between. I can't get the search to work. Here is the type of content and what I've tried at PHP Sandbox. I thought a non-greedy quantifier would work, but no joy.
$content = 'Weather: rainy</p>
<p> NOTE: You are entering the twilight zone:<br />
– No cell phones</p>
<p>Click here:<br />
<a href="/" rel="nofollow">/</a><br />
—<br />
To exit, visit:<br />
<a href=".php?page=twilight;subpage=global_options" rel="nofollow">.php?page=twilight;subpage=global_options</a><br />
To see current twilight zone conditions, visit:<br />';
$search = '~<p>\sNOTE[.\n\r]+?/</a><br />~';
$replace = '';
$content = preg_replace($search, $replace, $content);
echo $content;
I'm trying to alter the content of an email generated by a plugin, removing a lengthy section that starts with <p> NOTE:
(the space is actually a tab) and ending with /</a><br />
(lines 2-5 below). There are all sorts of characters and line feeds in between. I can't get the search to work. Here is the type of content and what I've tried at PHP Sandbox. I thought a non-greedy quantifier would work, but no joy.
$content = 'Weather: rainy</p>
<p> NOTE: You are entering the twilight zone:<br />
– No cell phones</p>
<p>Click here:<br />
<a href="https://www.example/" rel="nofollow">https://www.example/</a><br />
—<br />
To exit, visit:<br />
<a href="https://example/wp-admin/admin.php?page=twilight;subpage=global_options" rel="nofollow">https://example/wp-admin/admin.php?page=twilight;subpage=global_options</a><br />
To see current twilight zone conditions, visit:<br />';
$search = '~<p>\sNOTE[.\n\r]+?/</a><br />~';
$replace = '';
$content = preg_replace($search, $replace, $content);
echo $content;
Share
Improve this question
edited Dec 10, 2020 at 17:24
Jim Worrall
asked Dec 10, 2020 at 16:58
Jim WorrallJim Worrall
1777 bronze badges
6
|
Show 1 more comment
1 Answer
Reset to default 1I finally got it figured out. In order to see the content that I was trying to edit, I turned on debugging in wp-config.php:
define('WP_DEBUG', true); // set true
if ( WP_DEBUG ) {
define('WP_DEBUG_LOG', '../logs/debug.log' );
define('WP_DEBUG_DISPLAY', false); // don't display errors on pages
@ini_set( 'display_errors', 0 );
}
Then, in the function that does the replacing, I commented out that stuff and instead just put error_log( print_r( $content, true ) );
. When I triggered the email, the $content
then showed up in that log.
Turns out the content IS different when the filter is applied than in the final email raw source. The $content
has no html except for <a>
tags for links. Once I changed the search pattern to match what was there in the log, it worked perfectly!
本文标签: phppregreplace regex problem
版权声明:本文标题:php - preg_replace regex problem 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741926980a2405348.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
preg_replace
isn't a WordPress function it's a PHP core function. I'd suggest asking on stackoverflow, but they might respond by saying you should use an XMLParser instead of regular expressions as Regex only works for very simply HTML and can't fully parse HTML due to its structure. Have you contacted the plugin author? There's probably a hook you can remove – Tom J Nowell ♦ Commented Dec 10, 2020 at 17:11'~<p>\sNOTE(.|\s)+?/</a><br />~';
, though I don't understand why the original didn't work. – Jim Worrall Commented Dec 10, 2020 at 17:57'~\tNOTE(.|\s)+?/</a><br />~'
with nothing works as expected in PHP Sandbox, but it deletes the entire email content in Wordpress. Is it possible the email content when the filter is acting (mailtpl/email_content
from the Email Templates plugin) is different from the raw source content of the received email? – Jim Worrall Commented Dec 10, 2020 at 21:16