Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

preg_replace to preg_replace_callback


Recommended Posts

since regex and stuff is a book of seven seals to me, i would need some help to bring those two lines to php7.0

it should be transformed into preg_replace_callback because of the e modifier, but i dont understand how this preg_replace_callback works.

Can someone help and transform those lines for me? Thanks alot!

        $text = preg_replace("#\{\{((STORE_|OWNER_|EMAIL_)[A-Z0-9_]+)\}\}#e", '$1', $text);

        $email_subject = preg_replace("#\{\{((STORE_|OWNER_|EMAIL_)[A-Z0-9_]+)\}\}#e", '$1', $email_subject);

 

 

Link to comment
Share on other sites

Not knowing the full code for the function, Please make sure you back up before doing this! Not a PHP expert but try this for first line and see what error you get.

$text = preg_replace_callback("#\{\{((STORE_|OWNER_|EMAIL_)[A-Z0-9_]+)\}\}#", function ($matches) {return '$1'($matches['$1']); }, $text);

 

Link to comment
Share on other sites

Check your work carefully that you did not accidentally insert an extra ( or drop a needed ). It's easy to mistake a { for a (, or vice-versa, when reading code.

May I ask what sort of project you're doing? If you're trying to convert an old add-on to PHP 7 compatibility, that's one thing, but if you have an old osC (anything pre-2.3.4BS Edge) it's frankly not a good use of your time to try upgrading old code to work with PHP 7. You'd be much better off installing "Edge" (see link below in my signature) and migrating your data over. You'll have PHP 7.1 compatibility, lots of new features and bug fixes, and best of all it's mobile-friendly (responsive).

Link to comment
Share on other sites

the way the original preg_replace was coded was to replace {{STORE_OWNER}} etc with defined constants (its all in a payment module). so, this works now. i hope this is usefull to someone someday.
and please if you are asked if you can fix a small thing on an old car, dont try to sell a new car if you can just help with the problem. maybe i already have a new car, but still want to drive around a little longer with my old car as well. yes i know, the new car is the real deal! ;)

thank you @JcMagpie for your feedback/help!

 $text = preg_replace_callback(
    "#\{\{((STORE_|OWNER_|EMAIL_)[A-Z0-9_]+)\}\}#",
        function ($matches) {
            return constant($matches[1]);
        },
        $text
    );

another one that i have hopefully changed the right way in a helper function of this payment module is this one

        //$historyComments['seller'] = preg_replace('#\{\{([a-zA-Z0-9_]+)\}\}#e', '$$1', $historyComments['seller']);
        $historyComments['seller'] = preg_replace_callback(
        '#\{\{([a-zA-Z0-9_]+)\}\}#',
            function ($matches) {
            return ('$$matches[1]');
            },
        $historyComments['seller']
        );

 

 

Link to comment
Share on other sites

5 hours ago, Stephan Gebbers said:

the way the original preg_replace was coded was to replace {{STORE_OWNER}} etc with defined constants (its all in a payment module). so, this works now. i hope this is usefull to someone someday.
and please if you are asked if you can fix a small thing on an old car, dont try to sell a new car if you can just help with the problem. maybe i already have a new car, but still want to drive around a little longer with my old car as well. yes i know, the new car is the real deal! ;)

thank you @JcMagpie for your feedback/help!


 $text = preg_replace_callback(
    "#\{\{((STORE_|OWNER_|EMAIL_)[A-Z0-9_]+)\}\}#",
        function ($matches) {
            return constant($matches[1]);
        },
        $text
    );

another one that i have hopefully changed the right way in a helper function of this payment module is this one


        //$historyComments['seller'] = preg_replace('#\{\{([a-zA-Z0-9_]+)\}\}#e', '$$1', $historyComments['seller']);
        $historyComments['seller'] = preg_replace_callback(
        '#\{\{([a-zA-Z0-9_]+)\}\}#',
            function ($matches) {
            return ('$$matches[1]');
            },
        $historyComments['seller']
        );

Yes looks fine did not show any errors when I tested.:thumbsup:

 

Link to comment
Share on other sites

and please if you are asked if you can fix a small thing on an old car, dont try to sell a new car if you can just help with the problem. maybe i already have a new car, but still want to drive around a little longer with my old car as well. yes i know, the new car is the real deal! ;)

If you are trying to fix a broken headlamp on your old clunker, sure I'll be happy to give help. However, if you're trying to rebuild the engine of your daily driver one piece at a time by trial and error until it (sort of) runs, you'd be much better off with something new(er). You never said what you were trying to do, but it sure sounds like you're trying to do a major fix on old code, which as I said, will not be a productive use of your time. An old car is still compatible with the current highway system. An old application is no longer compatible with current servers. I can understand a sentimental attachment to a '65 Mustang; I can't understand keeping around osC 2.2. Your call.

Link to comment
Share on other sites

  • 3 weeks later...

Just found this thread here. I need to get the following code up to date since it doesn't work in PHP 7 anymore.
Getting this error message:

Quote

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead

I know that #se in the code below is the /e modifier and that's about it with my understanding.
Most support solutions I found on stackoverflow talkj about simple preg_replace cases but the following is a bit harder I think.

function tsimi ($text, $token, $class) {
$text = str_replace('\"', '"', substr(preg_replace('#(\>(((?>([^><]+|(?R)))*)\<))#se', "preg_replace('#\b(" . $token . ")\b#i', '<span class=\"" . $class . "\">\\\\1</span>', '\\0')", '>' . $text . '<'), 1, -1));
return $text;
}

Anyone out there can help getting this updated to a preg_replace_callback?

Thanks

Link to comment
Share on other sites

Doing a search ("all topics") on preg_replace_callback gave me 17 hits just on this forum. Were none of those sufficient? If not, I'm sure StackOverflow, among others (via Google), has a ton of suggestions. I'm not sure this is something that you can mechanically perform (i.e., it requires some understanding of what was being done before), but it should be reasonably straightforward.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...