multiple replacements using str_replace

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi all,

I'm attempting to use string replace to use escape characters in file
& pathnames. I need to be able to replace several characters, such as
a space and #. While I can replace any one of these with something

$file = str_replace(" ", "&#32", $row['filename']);

I need to be able to replace all with the appropriate escape
character. According to this page: , I
should be able to do something like the following:

// replace some characters with escape characters in the path &
$bad  = array(" ", "#");
$good = array(" ", "#");

$file = str_replace($bad, $good, $row['filename']);

But that doesn't seem to work. I end up getting URLs that literally
have "&#32" instead of spaces, and doesn't replace the #'s at all.

I'm sure it's just a stupid newbie mistake, but any help is
appreciated. Thanks.

Re: multiple replacements using str_replace

razed wrote:
Quoted text here. Click to load it

What are you really trying to do - maybe the same as urlencode()?

str_replace is doing exactly what you told it to do.  It is taking the
space character and replacing it with the characters ampersand,
octothorpe, digits three and two, terminating with a semicolon.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: multiple replacements using str_replace

Quoted text here. Click to load it

Thanks for the swift reply. urlencode() does look promising, and
sounds like it's what I'm trying to do, however it ends up just
replacing all the spaces in my filenames with +'s.

What I'm trying to do is read path/filenames from a MySQL database,
and build download links for each file in the database. I had the
table built, but I noticed that none of the download links were
working, because as soon as it encountered a space in the path, it
stopped building the link. For example, many of my download links were
being built by PHP as " " when in actuality they
should have been " Files/sample file.txt". I fixed
this initial problem by doing a str_replace on the path and filenames
for each file in order to turn the spaces into valid HTML escape

     $file = str_replace(" ", " ", $row['filename']);

This worked just fine, until I realized that several of the filenames
had # signs in them as well. So now I'm looking for a way to tell PHP
that when it sees a space, I want it to replace it with " ", and
when it sees a # sign, I want it to replace it with a "#".

This example from seemed to make me think that
putting the characters I wanted replaced into one array, while putting
the characters I wanted them replace with in another array would do
what I wanted:

     // Provides: You should eat pizza, beer, and ice cream every day
     $phrase  = "You should eat fruits, vegetables, and fiber every
     $healthy = array("fruits", "vegetables", "fiber");
     $yummy   = array("pizza", "beer", "ice cream");

     $newphrase = str_replace($healthy, $yummy, $phrase);

But, like I said in my first post, that doesn't seem to be working out
too well. :(

Re: multiple replacements using str_replace

writing in news:50aee064-4590-4113-acf4-8ffebd4828e5

Quoted text here. Click to load it

That's what it's supposed to do.  Look at the link below. See the +'s.  
Copy this string and put it into the URL bar of your browser.  See where
it takes you.

Adrienne Boswell at Home
Arbpen Web Site Design Services
Please respond to the group so others can share

Re: multiple replacements using str_replace

Quoted text here. Click to load it

Aha! I figured it out. I was using the wrong escape characters.
Instead of &#32 and &#35, I needed URL escape characters, which are
%20 and %23, respectively. Sorry for wasting your time, but thanks for
the replies! Sometimes it just helps to explain your problem to
someone, I suppose. :)

Re: multiple replacements using str_replace

Quoted text here. Click to load it


Quoted text here. Click to load it

Be careful with urlencode() and rawurlencode(), as you don't want to
blindly escape the whole URI.  If you want to use/display a link on a
Web page, use htmlspecialchars().

$email = str_replace('sig.invalid', '', $from);

Anonymous (1984 IOCCC winner):
int i;main()"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p)

Site Timeline