Title: Preg_replace not working
Last modified: September 1, 2016

---

# Preg_replace not working

 *  [diondkb](https://wordpress.org/support/users/diondkb/)
 * (@diondkb)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/preg_replace-not-working/)
 * Hi,
 * Is there any reason why preg_replace should yield different results if run on
   an online test tool like [http://regexr.com/](http://regexr.com/) as opposed 
   to running it in the wordpress context.
 * When this the following is run in the wordpress context, it finds a match and
   does the replacement, but not when run on two online regex tools.
 * The syntax is
 *     ```
       return preg_replace('/\.mp3(?!\?name)(?!"])/',
                                   '.mp3?name=[current_user]',
                                   $content
                               );
       ```
   
 * The string to search is:
    ‘[html5_audio src=”[https://s3.amazonaws.com/sfs-premium/collection/Rider+Aids/Seat+Aids+and+Feel/Learning+How+to+Stop+Driving+with+Your+Seat/686-Learning-to-Stop-Driving-with-Your-Seat.mp3″%5D&#8217](https://s3.amazonaws.com/sfs-premium/collection/Rider+Aids/Seat+Aids+and+Feel/Learning+How+to+Stop+Driving+with+Your+Seat/686-Learning-to-Stop-Driving-with-Your-Seat.mp3″%5D&#8217);
 * **Running the expression on the online tool, yields:**
    [html5_audio src=”[https://s3.amazonaws.com/sfs-premium/collection/Rider+Aids/Seat+Aids+and+Feel/Learning+How+to+Stop+Driving+with+Your+Seat/686-Learning-to-Stop-Driving-with-Your-Seat.mp3″%5D](https://s3.amazonaws.com/sfs-premium/collection/Rider+Aids/Seat+Aids+and+Feel/Learning+How+to+Stop+Driving+with+Your+Seat/686-Learning-to-Stop-Driving-with-Your-Seat.mp3″%5D)
 * **while run in the wordpress environment yields:**
    [html5_audio src=”[https://s3.amazonaws.com/sfs-premium/collection/Rider+Aids/Seat+Aids+and+Feel/Learning+How+to+Stop+Driving+with+Your+Seat/686-Learning-to-Stop-Driving-with-Your-Seat.mp3?name=%5Bcurrent_user%5D”%5D](https://s3.amazonaws.com/sfs-premium/collection/Rider+Aids/Seat+Aids+and+Feel/Learning+How+to+Stop+Driving+with+Your+Seat/686-Learning-to-Stop-Driving-with-Your-Seat.mp3?name=%5Bcurrent_user%5D”%5D)
 * Does wordpress add chars causing the opposite results?
 * Any help will be appreciated.
 * Dion

Viewing 12 replies - 1 through 12 (of 12 total)

 *  [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * (@rossmitchell)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/preg_replace-not-working/#post-7618946)
 * You are putting a leading and trailing “/” on your search string, these are part
   of the syntax for many editor commands, but here PHP just wants the search strings.
 *  Thread Starter [diondkb](https://wordpress.org/support/users/diondkb/)
 * (@diondkb)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/preg_replace-not-working/#post-7618949)
 * Hi. Not according to this example…
 *     ```
       <?php
       $string = 'April 15, 2003';
       $pattern = '/(\w+) (\d+), (\d+)/i';
       $replacement = '${1}1,$3';
       echo preg_replace($pattern, $replacement, $string);
       ?>
       ```
   
 * …found here [http://php.net/manual/en/function.preg-replace.php](http://php.net/manual/en/function.preg-replace.php)
 *  [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * (@rossmitchell)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/preg_replace-not-working/#post-7618953)
 * That example does not work for me ? Only has “/” at start.
    Please try it without
   the “/”.
 *  Thread Starter [diondkb](https://wordpress.org/support/users/diondkb/)
 * (@diondkb)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/preg_replace-not-working/#post-7618954)
 * Here is the code(placed in a shortcode)
 *     ```
       function add_user_to_mp3( $content )
       {
   
           return preg_replace('\.mp3(?!\?name)(?!"])',
                                   '.mp3?name=[current_user]',
                                   $content
                               );
   
       }
   
       add_filter( 'content_save_pre' , 'add_user_to_mp3' , 10, 1);
       ```
   
 * When I refresh the web page, I get the following error –
    Warning: preg_replace():
   Delimiter must not be alphanumeric or backslash in
 *  Thread Starter [diondkb](https://wordpress.org/support/users/diondkb/)
 * (@diondkb)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/preg_replace-not-working/#post-7618955)
 * I stuck the same piece of code into one of my web pages and got the correct output-
 *     ```
       <?php echo preg_replace('/\.mp3(?!\?name)(?!"])/',
                                   '.mp3?name=[current_user]',
                                   '[html5_audio src="https://s3.amazonaws.com/sfs-    premium/collection/Rider+Aids/Seat+Aids+and+Feel/Learning+How+to+Stop+Driving+with+Your+Seat/686-Learning-to-Stop-Driving-with-Your-Seat.mp3"]'
                               ); ?>
       ```
   
 * preg_replace does not seem to work correctly in the context of a shortcode?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/preg_replace-not-working/#post-7618966)
 * But your web page code is not the same as the shortcode handler code. In the 
   webpage version you’ve properly used ‘/’ as regexp delimiters, but not in the
   shortcode version. `preg_replace()` works the same in either context as long 
   as your regexp is properly formatted!
 * BTW, you can use any character as a delimiter as long as it does not occur in
   the regexp proper. Or if it does it must be escaped.
 *  Thread Starter [diondkb](https://wordpress.org/support/users/diondkb/)
 * (@diondkb)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/preg_replace-not-working/#post-7618987)
 * Thanks for your patience. I changed the shortcode as requested with no change-
 *     ```
       function add_user_to_mp3( $content )
       {
   
           return preg_replace('/\.mp3(?!\?name)(?!"])/',
                                   '.mp3?name=[current_user]',
                                   $content
                               );
   
       }
   
       add_filter( 'content_save_pre' , 'add_user_to_mp3' , 10, 1);
   
       After editing the page containing the shortcode in the wordpress editor, and adding this code  and then saving, the code changes from this:
       ```
   
 * </div>
 * [html5_audio src=”[https://s3.amazonaws.com/sfs-premium/collection/Rider+Aids/Seat+Aids+and+Feel/Learning+How+to+Stop+Driving+with+Your+Seat/686-Learning-to-Stop-Driving-with-Your-Seat.mp3″%5D](https://s3.amazonaws.com/sfs-premium/collection/Rider+Aids/Seat+Aids+and+Feel/Learning+How+to+Stop+Driving+with+Your+Seat/686-Learning-to-Stop-Driving-with-Your-Seat.mp3″%5D)
 *     ```
       to this:
       ```
   
 * </div>
 * [html5_audio src=”[https://s3.amazonaws.com/sfs-premium/collection/Rider+Aids/Seat+Aids+and+Feel/Learning+How+to+Stop+Driving+with+Your+Seat/686-Learning-to-Stop-Driving-with-Your-Seat.mp3?name=%5Bcurrent_user%5D”%5D](https://s3.amazonaws.com/sfs-premium/collection/Rider+Aids/Seat+Aids+and+Feel/Learning+How+to+Stop+Driving+with+Your+Seat/686-Learning-to-Stop-Driving-with-Your-Seat.mp3?name=%5Bcurrent_user%5D”%5D)
   `
   I copied the exact same preg_replace pattern(‘/\.mp3(?!\?name)(?!”])/’) from 
   the shortcode function(which at this point does not seem to work) and pasted 
   it in the web page mentioned previously and did a hard refresh and the pattern
   behaves as expected ie not doing the replacement(‘.mp3?name=[current_user]’)?
 * Any ideas I can try. Thanks
 *  Thread Starter [diondkb](https://wordpress.org/support/users/diondkb/)
 * (@diondkb)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/preg_replace-not-working/#post-7618988)
 * sorry. got the tags a little mixed up.
 *  [Maria Daniel Deepak](https://wordpress.org/support/users/mariadanieldeepak/)
 * (@mariadanieldeepak)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/preg_replace-not-working/#post-7618989)
 * [@diondkb](https://wordpress.org/support/users/diondkb/),
 * In addition to [@bcworkz](https://wordpress.org/support/users/bcworkz/) answer,
   PHP uses [PCRE functions](http://www.pcre.org/) and it requires you to use [delimiters](http://php.net/manual/en/regexp.reference.delimiters.php)
   when you’re finding patterns using regex.
 * Some of the commonly used delimiters are
 *     ```
       /foo bar/
       #^[^0-9]$#
       +php+
       %[a-zA-Z0-9_-]%
       ```
   
 *  [Maria Daniel Deepak](https://wordpress.org/support/users/mariadanieldeepak/)
 * (@mariadanieldeepak)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/preg_replace-not-working/#post-7618991)
 * [@diondkb](https://wordpress.org/support/users/diondkb/),
 * Could you tell us in simple terms what your end result should look like? (i.e.
   Your string and what you want achieve with it?)
 *  Thread Starter [diondkb](https://wordpress.org/support/users/diondkb/)
 * (@diondkb)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/preg_replace-not-working/#post-7619026)
 * Hi,
 * If the line of text ends in ‘.mp3″]’, then don’t do the replacement. So,
 *     ```
       [html5_audio src="https://s3.amazonaws.com/sfs-premium/collection/Rider+Aids/Seat+Aids+and+Feel/Learning+How+to+Stop+Driving+with+Your+Seat/686-Learning-to-Stop-Driving-with-Your-Seat.mp3"]
       ```
   
 * … would not change, while
 *     ```
       <audio src="https://s3.amazonaws.com/sfs-premium/collection/Rider+Aids/Seat+Aids+and+Feel/Learning+How+to+Stop+Driving+with+Your+Seat/686-Learning-to-Stop-Driving-with-Your-Seat.mp3" />
       ```
   
 * would be changed to look like this
 *     ```
       <audio src="https://s3.amazonaws.com/sfs-premium/collection/Rider+Aids/Seat+Aids+and+Feel/Learning+How+to+Stop+Driving+with+Your+Seat/686-Learning-to-Stop-Driving-with-Your-Seat.mp3?name=[current_user]"
       ```
   
 * Also, if the ‘?name=’ string comes immediately after the ‘.mp3’ not substitution
   is necessary.
 * Hope this makes sense.
 *  [Maria Daniel Deepak](https://wordpress.org/support/users/mariadanieldeepak/)
 * (@mariadanieldeepak)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/preg_replace-not-working/#post-7619045)
 * [@diondkb](https://wordpress.org/support/users/diondkb/),
 * Okay, got it.
 * In my opinion, you should try to write a regular expression to match the part
   of the string that you’re trying to replace.
 * Could you see if the following fits your need.
 * Regex – `/(.mp3)[\'|\"][ ]*\/\>/`
 * I have a done a working demo here ([http://www.phpliveregex.com/p/gBD](http://www.phpliveregex.com/p/gBD)).
   When you visit this page, click on the **preg_replace** tab.

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘Preg_replace not working’ is closed to new replies.

## Tags

 * [preg_replace](https://wordpress.org/support/topic-tag/preg_replace/)
 * [regex](https://wordpress.org/support/topic-tag/regex/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 12 replies
 * 4 participants
 * Last reply from: [Maria Daniel Deepak](https://wordpress.org/support/users/mariadanieldeepak/)
 * Last activity: [9 years, 9 months ago](https://wordpress.org/support/topic/preg_replace-not-working/#post-7619045)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
