How to Use PHP str_replace Function

The str_replace function is primary used to replace a PHP string with a variable or alternate text. Definition is: str_replace — Replace all occurrences of a search string with a replacement string.

So how do you use it for Joomla Web Development? First we need to fully understand the function and what variables it will accept. Lets take a look further: str_replace ( $search , $replace , $subject [, int &$count ] ). In this guide, we are not going to worry about the count portion of this function. Instead we will concentrate on 3 inputs: Search String, what to replace it with and where to put it within the Language File (en-GB.ini).

First, setup the variable that you want to import into the language file’s string. In this case we are going to replace the following string:”JGLOBAL_AUTH_NO_REDIRECT=”Could not redirect to server: {redirectURL}.”

Lets grab the current Referrer or Prior URL: $redirectURL = $_SERVER[‘HTTP_REFERER’]; Now lets change the string to the new URL:str_replace(‘{redirectURL}’, $redirectURL, JGLOBAL_AUTH_NO_REDIRECT);

Lets break down further the above example:

  1. We setup the variable
  2. We added the replace String Variable (That we will use in the language file). Another words, search in the string for {redirectURL} and replace it with the value of $redirectURL.
  3. Lastly, where should the function search. In this case the JGLOBAL_AUTH_NO_REDIRECT field in the Language file
    JGLOBAL_AUTH_NO_REDIRECT=”Could not redirect to server: {redirectURL}”
  4. New output would be: “Could not redirect to server:”

Good luck!

Cove Technology

Recent Posts

Mailchimp and Mandrill Database Issues

Over the next week your Contact Us Form may stop working. Why? MailChimp is having database issues and “it’s possible…

8 months ago

Should you update to iOS 12.2?

TL;DR It's imperative to update to iOS 12.2 as soon as you can. If you are unfamiliar with the FaceTime…

8 months ago

Ashton Kutcher’s Real Phone Number Partnership

As you're waking up, drinking your coffee and starting your day, news is breaking that Ashton Kuther released his personal…

8 months ago

Setting Up A Successful E-Commerce Business

Post written by Anne Farrimond The internet is a dream come true for a lot of budding entrepreneurs. Being able…

3 years ago

Update UConnect for Chrysler Vehicles 0Day Vulnerability

Its extremely important that you immediately update your Chrysler Vehicle's UConnect System. Why? Hackers Remotely Kill a Jeep on the…

4 years ago

MIT Providing Free Online Courses on Game Design

MITx: Design and Development Courses Want to learn how to build and develop the newest educational technology? MIT is now…

5 years ago