string replace issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

string replace issue

Post by sulen »

I want to replace the "\" with the "/" in a particular php string but cannot find a way to make it work for some reason. It will be greatly appreciated if someone could guide me. Thanks

I am using the following ways but it doesnt work

Code: Select all

$dir = ereg_replace('\', '/', $result[RequestResult][CourseDirPathName]);
I have even tried str_replace but to no use. Thanks
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

this is with STR replace, should work

Code: Select all

str_replace("\", "/", $string);
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

what nickman said would work...do not use ereg_replace instead use preg_replace for complex regex replacements but for this, str_replace is faster.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

and i think it is much more easier.
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

I tried the following code

Code: Select all

$dir = str_replace("\", "/", $result[RequestResult][CourseDirPathName]);
The error I get is Parse error: parse error in blah blah blah

Thanks
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

post the exact error message...
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

Parse error: parse error in /usr/local/apache/htdocs/homework/webserviceTest.php on line 69
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

If you have the parse messsage it will be easier for us to solve the problem.

try using quotes around your string that you want to replace, like this

Code: Select all

$dir = str_replace("\", "/", "$result[RequestResult][CourseDirPathName]");
EDIT
Post lines 68 and 69 please.
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

Dont worry got it to work.......... used this code

Code: Select all

$dir = ereg_replace('\\\\', '/', $result[RequestResult][CourseDirPathName]);
Thanks for all the help.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Cool.

I see you used ereg replace, that is fine. I personally think STR_replace is much easier.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

sulen wrote:Dont worry got it to work.......... used this code

Code: Select all

$dir = ereg_replace('\\\\', '/', $result[RequestResult][CourseDirPathName]);
Thanks for all the help.
I could not understand...what these four slashes mean?
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

LOL.

I was saying the same thing to myself.
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

I am surprised too but it seemed to work with the four slashes instead of one. Funny thing.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ahem

Code: Select all

$dir = str_replace('\\', '/', $string);
Post Reply