0

OK so i'm unsure with my syntax on this one, I do think the logic is self explanatory but I'm unsure if preg_replace will work or whether a loop is needed to get an end result.

$string = $randomizer->fecthRandomPhrase($cfg['seo']['meta']['descriptions']['single'], 3, $_SERVER['REQUEST_URI']);

Returns a string like this

Lorem ipsum dolor sit amet,[address1], [address2], [postcode]. consectetur adipiscing elit. Mauris id dui sem, eget laoreet tellus. Vivamus lacinia vestibulum odio a lobortis - [region]

I then search the string for the parts I want to change;

$find = array('[address1]','[address2]','[postcode]','[region]');

I then pull information stored in these variables and place them in an array;

$replace = array($ADDRESS1,$ADDRESS2,$POSTCODE,$region);

The before returning the phrase I apply a preg_replace to swap over the info I have stored

$phrase = preg_replace($find,$replace,$string);

Do I need to loop through the array $replace to allow the reading of each variable and for the replace to work or am I using the wrong function entirely?

1
  • Thanks for tidying that up Antony, I knew there would be a spelling mistake or missing code block somehow. (flux on screen makes it harder to see the grey boxes lol) Commented May 14, 2013 at 16:23

3 Answers 3

2

str_replace() can take arrays as its first two parameters, you might want to consider that instead. Otherwise, you'd need to form a proper regex for $find in order to only invoke preg_replace() once, which you currently are not doing.

Usage:

$phrase = str_replace( $find, $replace, $string);

Now $phrase shoud contain your desired output.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah! Thanks for that i'll make sure to use str_replace when passing arrays this did the trick. Thanks nickb
2

use PHP's str_replace function like this

str_replace(array($your_replacement_array), array($your_replace_array), $string);

Hence what you want is this

str_replace(array('[address1]','[address2]','[postcode]',['region']), array($ADDRESS1,$ADDRESS2,$POSTCODE,$region), $string);

Comments

0

strtr is also useful.

On the following situation, only strtr works exptectedly.

$text = 'SonyEricsson is based in British, but Sony is based in Japan';

str_replace

code:

$search  = array(
    'SonyEricsson',
    'Sony',
);
$replace = array(
    '<a href="http://www.SonyEricsson.com">SonyEricsson</a>',
    '<a href="http://www.Sony.com">Sony</a>',
);
echo str_replace($search,$replace,$text);

result:

<a href="http://www.<a href="http://www.Sony.com">Sony</a>Ericsson.com"><a href="http://www.Sony.com">Sony</a>Ericsson</a> is based in British, but <a href="http://www.Sony.com">Sony</a> is based in Japan

strtr

code:

$replace_pairs = array(
    'SonyEricsson' => '<a href="http://www.SonyEricsson.com">SonyEricsson</a>',
    'Sony'         => '<a href="http://www.Sony.com">Sony</a>',
);
echo strtr($text,$replace_pairs);

result:

<a href="http://www.SonyeEricsson.com">SonyEricsson</a> is based in British, but <a href="http://www.Sony.com">Sony</a> is based in Japan

2 Comments

I'll try to work this method with my next task as the next group of variables I need to replace are 3d arrays, I'm needing to replace more than once piece of information about a building such as the case with my example in the question I assume it would be easier to find all of the text I need to replace rather than splitting it up into multiple variables?
Are you meaning that sending paired arguments like strtr is superior to sending splitted arguments like str_replace?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.