#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0
    [[ split from http://forums.devshed.com/php-development-5/preg-replace-nothing-to-repeat-at-offset-285631.html --requinix ]]

    Hello n.eby,
    I know a long time has passed since your post, but I'm having exactly the problem you have described.
    This is the code;
    PHP Code:
        public function replaceRequest(&$msg)
        {
            
    $request JRequest::get('request');
            foreach (
    $request as $key => $val)
            {
                if (
    is_string($val))
                {
                    
    // $$$ hugh - escape the key so preg_replace won't puke if key contains /
                    
    $key str_replace('/''\/'$key);
                             echo 
    "key = $key <br />";
                    
    $msg preg_replace("/\{$key\}/"$val$msg);
                }
            }
        } 
    The echo was placed in there to view the values of $key, these are the values;

    key = option
    key = view
    key = listid
    key = Itemid
    key = http:\/\/portal_oceanisweb_eu\/administrator\/**http:\/\/fabrikar_com\/wiki\/

    Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 49 in /home/content/19/10378319/html/portal/components/com_fabrik/helpers/parent.php on line 629
    key = jpanesliders_content-sliders-264
    key = jpanesliders_permissions-sliders-264
    etc.other values

    As you can see the value of "key" causing the problems has "**" which I think is the cause.
    I'm not sure where this value is coming from or how to parse out the **
    As you can tell I'm a newbie at all this so please be patient.
    I'd appreciate any info you could give me.
    Last edited by requinix; April 21st, 2013 at 08:03 AM.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    do you really expect to get an answer from someone who has written a post in 2005 and hasn't visited the board since 2009? If you wanna wait, go ahead. It may take a while. If not, please make your own threads rather than digging out stuff from 8 years ago. This is a forum, so there are many active members who can help you.

    As to the question: Use preg_quote() to escape all characters that are meaningful in a regular expression (including the "*").
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,294
    Rep Power
    9400
    Like Jacques said, it really is better to just make your own thread for your own question and let the older thread be.


    It looks like you're using preg_replace() to replace simple text strings. Is that right? Then you should be using str_replace() instead, just like how you use it to "escape" the slash.
    PHP Code:
    public function replaceRequest(&$msg
        { 
            
    $request JRequest::get('request'); 
            foreach (
    $request as $key => $val
            { 
                if (
    is_string($val)) 
                { 
                    
    $msg str_replace("\{$key\}"$val$msg);
                } 
            } 
        } 
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    You're right about the str_replace(). But the search string would be
    Code:
    "{$key}"
    Since the backslashes were only for escaping within the regex.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,294
    Rep Power
    9400
    Ah, peculiar. One would expect the "escaped" {}s to result in a string
    Code:
    {$key}
    but they don't.
    [edit] But then preg_replace() would have barfed on the { instead of the **... [/edit]

    So yes, it should be more like
    PHP Code:
    "{{$key}}" 
    Or, to avoid potential "because {$var} is just another syntax for variable interpolation" confusion,
    PHP Code:
    "{" $key "}" 
    Last edited by requinix; April 21st, 2013 at 08:29 AM.

IMN logo majestic logo threadwatch logo seochat tools logo