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:

We setup the variable

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.

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}”

New output would be: “Could not redirect to server: http://www.covetech.net/blog”