I would like to know if there is any way to just remove backslashes '\' from a string with PHP? For some reason, when I use mysqli_real_escape_string() the output text has a '\' before any apostrophes. I know I could you use a function to strip the slashes but I want to keep all forward slashes '/'.
Btw, I've already made sure magic quotes is turned off. The problem still persists.
Thanks
There is a function for that and it is surprisingly called stripslashes
Here it goes: http://php.net/manual/en/function.stripslashes.php
It will keep your forward slashes if that's what you're worrying about
QUERY:-remove backslashes '\' from a string but I want to keep all forward slashes '/'.
After hard working and googled if find result of your query:-
$path="uploads\\folder_name\\ankur/like you"; /////string
echo str_replace("\\"," ",$path);
output:-uploads folder_name ankur/like you
Related
I've made some regex to test for a YouTube embedded video:
/^(http:\/\/www\.youtube\.com\/embed\/)[^\/\s\\]+$/
It works for what I expect when I test it, but the problem though is that I need to pass that regex as a string to some function. Particularly I'm using htmlawed, where I pass a following string to a function:
func('iframe=-*,src(match="/^(http:\/\/www\.youtube\.com\/embed\/)[^\/\s\\]+$/")');
The problem is that the above regex sort of works, but it just ignores the slashes, and accepts anything in place of them.
That is why I suspect that there is a problem with escaping.
I would appreciate if you could advice some alternative ways of escaping these slashes and backslashes... there must be some way?
If you have a string, you will need to escape the backslashes (and quotes) for the string literal. Or, depending on how the function builds the regex from the string, you might not need to escape slashes at all (I don't think so here).
"iframe=-*,src(match=\"/^(http:\\/\\/www\\.youtube\\.com\\/embed\\/)[^\\/\\s\\\\]+$/\")"
In PHP, you can also use a different regex delimiter:
~^(http://www\.youtube\.com/embed/)[^/\s\\\\]+$~
I've made some regex to test for a YouTube embedded video:
/^(http:\/\/www\.youtube\.com\/embed\/)[^\/\s\\]+$/
It works for what I expect when I test it, but the problem though is that I need to pass that regex as a string to some function. Particularly I'm using htmlawed, where I pass a following string to a function:
func('iframe=-*,src(match="/^(http:\/\/www\.youtube\.com\/embed\/)[^\/\s\\]+$/")');
The problem is that the above regex sort of works, but it just ignores the slashes, and accepts anything in place of them.
That is why I suspect that there is a problem with escaping.
I would appreciate if you could advice some alternative ways of escaping these slashes and backslashes... there must be some way?
If you have a string, you will need to escape the backslashes (and quotes) for the string literal. Or, depending on how the function builds the regex from the string, you might not need to escape slashes at all (I don't think so here).
"iframe=-*,src(match=\"/^(http:\\/\\/www\\.youtube\\.com\\/embed\\/)[^\\/\\s\\\\]+$/\")"
In PHP, you can also use a different regex delimiter:
~^(http://www\.youtube\.com/embed/)[^/\s\\\\]+$~
I'm trying to delete a file this way:
define('DESTINATION_FOLDER','c:\temp\\');
unlink(DESTINATION_FOLDER.$dest_filename);
And $dest_filename is something like this: 2jfioj23488hgh83hr.zip
But this doesn't work. When I put the path in a variable and echo it, it prints:
c:\temp\2jfioj23488hgh83hr.zip
Then I copy/paste it in unlink():
unlink('c:\temp\2jfioj23488hgh83hr.zip')
And it works. I think the problem is with the double backslashes. But how should I define the path without using the double backslashes? I don't know why it doesn't work. The line just above this code is:
copy((DESTINATION_FOLDER.$dest_filename),($extract.'\\'.$dest_filename));
And it works fine, but unlink doesn't work with the same syntax.
Putting my comment as an answer to the question
Try using forward slashes instead of backslash :)
If for some reason you must use backslashes, then you need to use c:\\ instead of c:\
define('DESTINATION_FOLDER','c:\\temp\\');
I have a file on my server that i want to access. The filename is ken\'s book.doc
But in my db, it was stored as ken's book.doc
(I have fixed the backslash issue, but still have problems accessing the previously uploaded files on server.
I used addslashes to add the back slash but it displays it as: ken/'s book.doc (that is a forward slash instead of a backslash.
I have used:
str_replace("'", "\'", $filename);
yet it displays as a forward slash.
How can i fix this?
Thanks
EDIT
Extra Information: I am using the new value as part of a link. that is:
View
If you have a filename that contains a backslash on disk, I would fix that first. Your second problem was appearantly not using mysql_real_escape_string when storing that filename into the database (why it ended up there without backslash).
addslashes btw does not add forward slashes by itself. That part of your story is untrue. And to remove them again you wouldn't need the quirky str_replace call, but just stripslashes.
The actual problem (after your edit) turns out to be a html link. That's simply because browsers have the habit of turning backslashes into forward slashes in urls. To prevent that apply urlencode()
View
Okay so im working on this php image upload system but for some reason internet explorer turns my basepath into the same path, but with double backslashes instead of one; ie:
C:\\Documents and Settings\\kasper\\Bureaublad\\24.jpg
This needs to become C:\Documents and Settings\kasper\Bureaublad\24.jpg.
Note that you may be running into PHP's Magic Quotes "feature" where incoming backslashes are turned to \\.
See
http://us2.php.net/magic_quotes
Use the stripslashes function.
That should make them all single slashes.
Have you considered the stripslashes() function?
http://www.php.net/stripslashes