1,105,371 Community Members

replace arrays issue

Member Avatar
mpc123
Junior Poster
160 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

Change the wrapping quotes to double quotes:

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

Or escape the quote:

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

and it should work.

Member Avatar
mpc123
Junior Poster
160 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

no that didnt work on either

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article