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 />
 &#8211; No cell phones</p>
<p>Click here:<br />
<a href="/" rel="nofollow">/</a><br />
&#8212;<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 />
 &#8211; No cell phones</p>
<p>Click here:<br />
<a href="https://www.example/" rel="nofollow">https://www.example/</a><br />
&#8212;<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
  • is the text always the same? This looks like a generic PHP question not a WordPress question, and 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
  • I think, yes, the text is always the same, though not as shown for example. I finally got it to work with this search code '~<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
  • Oops, correction. It worked in PHP Sandbox (php 7.4.0), but not on the actual website (7.4.13). Another preg_replace change I'm making to the same email does work on the site. – Jim Worrall Commented Dec 10, 2020 at 18:16
  • So I think now it is a Wordpress issue since it works in pure php but not in Wordpress. – Jim Worrall Commented Dec 10, 2020 at 18:27
  • Replacing '~\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
 |  Show 1 more comment

1 Answer 1

Reset to default 1

I 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