Removing
tags around WordPress Shortcodes while retaining wpautop() changes.

Problem:
I’m building something using Drew Morris’ Foundation 4 Theme for WordPress (http://fwp.drewsymo.com/) and it allows shortcodes like [row] and [column] to be used, so that content can be aligned to the grid.

The problem is, when those shortcodes are used with any sort of line break after them, wpautop() plays havoc with the markup. I could just disable wpauto() with `remove_filter( ‘the_content’, ‘wpautop’ );` but I want it to effect all other content.

Solution:
Wordpress 3.6 has an nice new tag called has_shortcode() which can be used along with some RegEx and preg_replace() to replace the offending <br /> tags. Here’s my first version… it can be improved but it’s working pretty well so far!

add_filter ('the_content', 'cleangridshortcodes');
function cleangridshortcodes($content) {
  if(has_shortcode($content,'row') || has_shortcode($content,'column')) {
    $patterns = array("/([row])
/","/([column(.*)])
/","/([/column])
/"); $replacements = array('$1','$1','$1'); $content = preg_replace($patterns, $replacements, $content); } return $content; }

Convert eregi_replace to preg_replace in old class.phpmailer.php scripts

Problem: I have lots of legacy php code on old sites that uses eregi_replace to format up an HTML email body. As eregi_replace is now depreciated it can be replaced with preg_replace. This is tricky sometimes because the formatting is quite different.

Fix: Here’s a common line from class.phpmailer.php scripts:

$emailBody = eregi_replace("[]",'',$emailBody);

To convert that you’ll need to do this:

$emailBody = preg_replace("/\\/", '', $emailBody);

Wondering why there are so MANY escaping backslashes there? I asked that question and got this answer:

“..the backslash needs to be escaped once for the php string’s benefit, then again for the interpretation of the regular expression engine, as escaped characters like d indicate a digit in regular expressions. So a pattern of \d would match a digit, but \\d would match a backslash then a d character. PHP strings are lenient on backslashing when it isn’t necessary, so:
– setting a string to “d” will give it a value of d (the same as setting it to “\d”).
– but setting it to “”” will give a value of “.
– and setting it to “” will return a syntax error.

– PHP double quoted strings will consume the (first) backslash for \ n t r and “.
– PHP single quoted strings will only consume the first backslash for \ and ‘ “

There.. clear as mud!

Textpad Regular Expressions

TextPad is a great editor, been using it for years, but never really used it to it’s full potential. Recently I needed to do some find/replace work in some huge SQL and CSV files – huge enough to make manual editing impossible – so I had to start using Textpad’s Regular Expression capabilities.

Here are a few expressions that came in handy. I intend to add to this list as time goes on.
Regexp shown in Green, my comments in Red:

CSV Editing:

Remove spaces and tab characters at the beginning of a line:
Find what: <span style="color: #008000;">^[ t]+</span>
Replace With: <span style="color: #ff0000;">don't enter anything in the field</span>

Remove spaces and tab characters at the end of a line:
Find what: <span style="color: #008000;">[ t]+$</span>
Replace With: <span style="color: #ff0000;">don't enter anything in the field</span>

Add “Hello” to the beginning of every line:
Find what: <span style="color: #008000;">(^.*)</span>
Replace With: <span style="color: #008000;">Hello 1</span>

Add “Hello ” to the beginning and ” World” to the end of every line:
Find what: <span style="color: #008000;">(^.*)</span>
Replace With: <span style="color: #008000;">Hello 1 World</span> <span style="color: #ff0000;">(watch the spaces)</span>

Find empty fields (i.e. “, ,”) with spaces or tabs in, and replace with empty field (“,,”):
Find what: <span style="color: #008000;">,[ t*],</span>
Replace With: <span style="color: #008000;">,,</span> <span style="color: #ff0000;">(just that, nothing else, just 2 commas)</span>

Remove blank lines in a text file, by searching for two linebreaks next to each other, and replacing with one:
Find what: <span style="color: #008000;">nn</span>
Replace With: <span style="color: #008000;">n</span>

Replacement Expressions:

Extract email addresses only from the following text: “Joe Blogs (job.blogs@blogsworld.com)”
This expression searches for 2 tagged expressions, firstly any printable characters including spaces up to the first open bracket symbol, secondly anything between the brackets. It then replaces the whole line with the second match.
Find what: <span style="color: #008000;">^([[:graph:] ]+)(([[:graph:] ]+))</span>
Replace With: <span style="color: #008000;">2</span>

Submitted by Paul: “Here’s how to convert csv to xml with a (quite large) regexp you can alter/extend for your needs.”
Find what: <span style="color: #008000;">^"([[:print:]]+)","([[:print:]]+)","([[:print:]]+)"</span>
Replace With: <span style="color: #008000;">123</span>

Notes:

  • The expressions above need the ‘Regular Expression’ condition to be ticked in the Find or Replace dialogue boxes for them to work.
  • Text pad needs to be set to use UNIX not POSIX style expressions. To set this, open ‘Configure > Preferences’ (Ctrl+Q,P), find the ‘Editor’ settings, and untick the ‘Use POSIX regular expression syntax’ box.

Please let me know if this post was useful with a click!
Nope, not helpful...Yep, more useful than not! - +27 thumb, 29 overall.
Loading...