1.11M Members

replace arrays issue

 
0
 

Hi I have been using a replace array which has worked wonders but now I have just added one and it wont work why is this please. I can understand why it wont work but do not know what i have to do to sort it. I added the last one to replace a ' with a -

$old = array(' ', '/', 'amp;', ''');
$new = array('-', '-', '-', '-');

Thanks for your help.

 
0
 

Change the wrapping quotes to double quotes:

$old = array(' ', '/', 'amp;', "'");

Or escape the quote:

$old = array(' ', '/', 'amp;', '\'');

and it should work.

 
0
 

no that didnt work on either

 
0
 

Can you provide some code? Here's my test:

<?php

$string = "Hello we're checking &amp; testing this brand-new /string/";

$old = array(' ', '/', 'amp;', "'");
$new = array('-', '-', '-', '-');

$string = str_replace($old, $new, $string);
$string = implode('-', array_filter(explode('-',$string)));
echo $string;
echo PHP_EOL;

Outputs: Hello-we-re-checking-&-testing-this-brand-new-string

It works also without $new array, since the character to replace is the same for all:

echo str_replace($old, '-', $string);
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article