TransWikia.com

preg_replace regex problem

WordPress Development Asked by Jim Worrall on January 23, 2021

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.com/" rel="nofollow">https://www.example.com/</a><br />
—<br />
To exit, visit:<br />
<a href="https://example.org/wp-admin/admin.php?page=twilight;subpage=global_options" rel="nofollow">https://example.org/wp-admin/admin.php?page=twilight;subpage=global_options</a><br />
To see current twilight zone conditions, visit:<br />';

$search = '~<p>sNOTE[.nr]+?/</a><br />~';
$replace = '';
$content = preg_replace($search, $replace, $content);
echo $content;

One Answer

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!

Answered by Jim Worrall on January 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP