0
$search = 'C:\xampp1.7.7\htdocs\myproject\uploads/files/temp-ds-original';
$subject = 'C:\xampp1.7.7\htdocs\myproject\uploads\files\temp-ds-original\32bd76470cff973ec873d43a4e84dd2f.jpg';    
echo str_replace($search, '', $subject);

It just prints $subject without doing any replacements. I thought it could be due to some php version issue as it was on a php 5.3 but then I moved to php 7.2 but still the same result. Not sure what's going wrong here?

Is it something to do with the slashes?

I have hardcoded string values above but in the actual script, I am using $f->getRealPath() to get subject and search. $f is an object of RecursiveIteratorIterator

EDIT

As soon as I posted this question, I could spot the issue as code highlighting made it quite clearer to see that slashes don't match - which means str_replace considers it a non-match. What I am trying to achieve is get relative path which in above example is \32bd76470cff973ec873d43a4e84dd2f.jpg ... the code is here at line 48 https://gist.github.com/bubba-h57/5117694

The above output is on a Windows machine but I will be using this script later on a Linux server. So I need to think about how to get the paths consistent so that str_replace can do the replacement correctly. $search is something I provide manually where $subject is being retrieved automatically using $f->getRealPath().

Update and Answer of my question I don't believe this question is duplicate to the linked question. People are quick here to show off their skills without paying due attention to details. :)

It turned out to be a simple solution. All I need to do is use realpath() i.e. $search = realpath($search); which gives me the correct result.

NoNice
  • 391
  • 1
  • 2
  • 14
  • Actually I seem to have spotted the issue as soon as I posted the question as code highlighting made it clearer to see. My source does have different type of slashes which is creating a no-match so no replacement. Now I need to worry about how to get the slashes consistent in $search as $subject is from getRealPath() – NoNice Apr 27 '18 at 09:03
  • 1
    Two slashes are wrong in $search String . $search = 'C:\xampp1.7.7\htdocs\myproject\uploads\files\temp-ds-original'; – milan kumar Apr 27 '18 at 09:04
  • These strings (paths) are not equal. `/` is not the same as a backslash \ – Qirel Apr 27 '18 at 09:06
  • My issue is at the moment I am doing this test on Windows machine, but later on I will be uploading script to Linux server - how do I get slashes right? – NoNice Apr 27 '18 at 09:08
  • I think we should focus on the problem rather than the question. There're specific functions to manipulate file system paths. What do you want to do exactly? – Álvaro González Apr 27 '18 at 09:10
  • Are you trying to get the file name of .jpg? – Daniel1147 Apr 27 '18 at 09:14
  • Yes - please see my update. – NoNice Apr 27 '18 at 09:18
  • Do you want the filename and the extension or just the filename – RiggsFolly Apr 27 '18 at 09:29
  • _People are quick here to show off their skills without paying due attention to details_ Or possibly you did not describe you required result very well! – RiggsFolly Apr 27 '18 at 09:33
  • No - I know how to get filename - see my update above. Thx everyone. – NoNice Apr 27 '18 at 09:34
  • @RiggsFolly - or you can ask them before deciding? I do think I put enough info there - you just gotta give a bit of time to add all the details. – NoNice Apr 27 '18 at 09:35
  • That time is supposed to be taken as you write your question – RiggsFolly Apr 27 '18 at 09:39
  • Yes I did take my time. If you read again I tried to resolve issue on my and even upgraded php because I didn't realise it was mismatch issue. I didn't ask for how to file filename from a given path.... my question was related to replacement and there was a specific purpose to do the replacement. I did update my questions and added comments as soon I progressed further. You are supposed to take the time to assess the requirements too before marking and closing questions. You closed this question way after I had posted my comments about what the actual issue is. – NoNice Apr 27 '18 at 09:43
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. – RiggsFolly Apr 27 '18 at 09:47
  • @mickmackusa I have no issue with closing but linked question doesnt resolve my issue so i find that incorrect. – NoNice Apr 27 '18 at 10:28
  • @RiggsFolly ok thanks I'll keep that in mind. – NoNice Apr 27 '18 at 10:40
  • You may post your answer as an answer and accept it. – mickmackusa Apr 27 '18 at 11:11
  • Yes I have done that now. – NoNice Apr 27 '18 at 12:13

3 Answers3

2

Just so that it helps anyone -

$search = 'C:\xampp1.7.7\htdocs\myproject\uploads/files/temp-ds-original';
$subject = 'C:\xampp1.7.7\htdocs\myproject\uploads\files\temp-ds-original\32bd76470cff973ec873d43a4e84dd2f.jpg';    
echo str_replace($search, '', $subject);

Output was:

C:\xampp1.7.7\htdocs\myproject\uploads\files\temp-ds-original\32bd76470cff973ec873d43a4e84dd2f.jpg

However I was expecting to be:

\32bd76470cff973ec873d43a4e84dd2f.jpg

I failed to notice the slashes mismatch and therefore str_replace was not at fault at. Importantly, I wasn't trying to get the filename only which I could get from basename() or other methods so I needed to get the slashes right.

All I needed to was to use PHP's realpath() i.e.

$search = realpath($search); 

That's it. However, you need to be careful that it only worked for me because I was parsing an actual path i.e. the folder in the $search existed on the disk. So, if you tried to parse a path string which is dummy or not a real directory, realpath() would return empty or false.

NoNice
  • 391
  • 1
  • 2
  • 14
0

you have to do this

enter image description here

you have to use doble backslash if you use only one dont work !

result:

enter image description here

-1

You could just use the same search always

(eg: $search = 'C:\xampp1.7.7\htdocs\myproject\uploads\files\temp-ds-original';)

then change the subject's slashes by using str_replace('/','\',$subject);

Or you could detect the OS and then use the matching $search

You can do this by checking the PHP Constant PHP_OS (Documentation in the link)

I hope that solves it.

Community
  • 1
  • 1
Yak0d3
  • 1
  • 3
  • See my update ... I can just do $search = realpath($search) which takes care of the slashes. – NoNice Apr 27 '18 at 09:32