Viewing 12 replies - 1 through 12 (of 12 total)
  • 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

    (@diondkb)

    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 https://php.net/manual/en/function.preg-replace.php

    That example does not work for me ? Only has “/” at start.
    Please try it without the “/”.

    Thread Starter diondkb

    (@diondkb)

    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

    (@diondkb)

    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

    (@bcworkz)

    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

    (@diondkb)

    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

    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
    `
    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

    (@diondkb)

    sorry. got the tags a little mixed up.

    @diondkb,

    In addition to @bcworkz answer, PHP uses PCRE functions and it requires you to use delimiters when you’re finding patterns using regex.

    Some of the commonly used delimiters are

    /foo bar/
    #^[^0-9]$#
    +php+
    %[a-zA-Z0-9_-]%

    @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

    (@diondkb)

    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.

    @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 (https://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.