[wp-trac] [WordPress Trac] #5624: Calls to preg_replace() after preg_match() in functions.php may be unnecessary

WordPress Trac wp-trac at lists.automattic.com
Thu Jan 10 07:59:06 GMT 2008


#5624: Calls to preg_replace() after preg_match() in functions.php may be
unnecessary
--------------------------+-------------------------------------------------
 Reporter:  bernzilla     |       Owner:  anonymous
     Type:  enhancement   |      Status:  new      
 Priority:  normal        |   Milestone:  2.6      
Component:  Optimization  |     Version:           
 Severity:  minor         |    Keywords:           
--------------------------+-------------------------------------------------
 I noticed that in functions.php (which comes with the WordPress.org
 install) there are at least a few instances where PHP's preg_replace()
 function is used to remove excess information to narrow in on a subset
 searched for in a preceding call to preg_match().

 Unless there is a specific reason for relying on preg_replace() in these
 instances, it might be better to rely on the subpattern match instead of
 the full string match, so that calls to preg_replace() can be avoided
 altogether.

 Consider the following example:

 {{{
 function xmlrpc_getposttitle($content) {
         global $post_default_title;
         if ( preg_match('/<title>(.+?)<\/title>/is', $content,
 $matchtitle) ) {
                 $post_title = $matchtitle[0];
                 $post_title = preg_replace('/<title>/si', '',
 $post_title);
                 $post_title = preg_replace('/<\/title>/si', '',
 $post_title);
         } else {
                 $post_title = $post_default_title;
         }
         return $post_title;
 }
 }}}

 The same effect can be accomplished by simply originally setting the
 $post_title variable to the subpattern match, like so:

 {{{
 function xmlrpc_getposttitle($content) {
         global $post_default_title;
         if ( preg_match('/<title>(.+?)<\/title>/is', $content,
 $matchtitle) ) {
                 $post_title = $matchtitle[1];
         } else {
                 $post_title = $post_default_title;
         }
         return $post_title;
 }
 }}}

 There may be other places in functions.php (or other files) where the same
 paradigm is employed.  If so, they could likely benefit from this change
 as well.

 It's a minor thing, but I figured I'd point it out anyway.

-- 
Ticket URL: <http://trac.wordpress.org/ticket/5624>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list