Forum Moderators: coopster

Message Too Old, No Replies

Removing a period "." from a string? How do I do it please?

i.e. ereg_replace(".", $string); doesn't work :/

         

crowthercm

8:13 pm on Aug 8, 2003 (gmt 0)

10+ Year Member



Hello,

Can someone please give me the syntax to deal with reserved characters in php? I'm having the following problem.

$string = "./blah/bloo/bling";

//I'd like to remove the starting "." to produce
// "/blah/bloo/bling"

// Does not work
ereg_replace(".", $string);

Can someone please tell me how this is done?

Thanks,
Chris

jatar_k

8:20 pm on Aug 8, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$newstring = str_replace [ca.php.net](".","",$string);

crowthercm

8:45 pm on Aug 8, 2003 (gmt 0)

10+ Year Member



Great, thanks (:

dvduval

8:55 pm on Aug 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



strrep also accepts array. So if you needed to replace multiple characters, you could do it like this:

$newstring = str_replace(array('.',''), array(',', ' '), $string);

toothfish

6:47 am on Aug 11, 2003 (gmt 0)

10+ Year Member



this is sort of related-- I'm trying to replace all instances of "--" with proper em-dashes with

<?php
$stuff="blah blah-- blah";
$patterns[0] = "--";
$replacements[0] = "&#8212;";
$newstuff= str_replace($patterns, $replacements, $stuff);
echo $newstuff;?>

And it doesn't seem to be working-- it just echoes the unaltered $stuff. The php manual just confused me. Any ideas?

Timotheos

5:18 pm on Aug 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi toothfish,

I'd be suspicious of this line with the ; in the string as php my be thinking that the ; in the your string is suppossed to terminated the line
$replacements[0] = "&#8212;";

try escaping it
$replacements[0] = "&#8212\;";

or try something else to test it like
$replacements[0] = "test";

toothfish

6:32 pm on Aug 11, 2003 (gmt 0)

10+ Year Member



Hello Timotheos. I noticed that right after I posted it but I have confirmed now that that's not the problem-- I tried escaping the semicolon out but it didn't seem to make any difference. Also the script doesn't choke as I would suspect it would if a line terminated prematurely. My guess is that I'm phrasing the search portion incorrectly-- the

$patterns[0] = "--";

part. Hyphens don't seem to have special functions where str_replace is concerned though, and it isn't working with either single or double quotes.

dvduval

6:40 pm on Aug 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In order to debug, why don't you try:

echoing everything
inserting the actual characters into str_replace()

Timotheos

7:21 pm on Aug 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Howdy toothfish,

I just tried that snippet of yours and it worked great.

sooo.... what version of php are you running? This is from the manual

In PHP 4.0.5 and later, every parameter to str_replace() can be an array.

If this is the case then make sure your $patterns and $replacements are not arrays or else upgrade your php.

toothfish

7:28 pm on Aug 11, 2003 (gmt 0)

10+ Year Member



Hmm. Things are echoing just fine (ie "--", "&8212;"). phpversion() returns 4.3.1, so it looks like that's not the problem.

$newstuff = str_replace("--","&8212;",$stuff);

does the same thing.

I saw some examples of ereg_replace whose search patterns looked like "/blah/" instead of "blah"-- I think my search syntax is what's wrong but I don't know how to fix it.

toothfish

7:32 pm on Aug 11, 2003 (gmt 0)

10+ Year Member




...snippet of yours and it worked great.

Oh man. I just tried it myself too and by itself it works fine, of course. I wonder what's going on?

Timotheos

12:13 am on Aug 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm stumped.