[solved]strrchr of a '\' doesn't work!

Alles, was PHP betrifft, kann hier besprochen werden.

[solved]strrchr of a '\' doesn't work!

Postby mickeymouse » 15. March 2012 00:43

I am trying to find the last occurance of a backslash in a file path so that I can then pickup the file name.
This
$cntr=strrchr($myfilepath,"\");
is what I'm doing to find this backslash but it wont work. I get an error msg which refers to some place further in my code.

Parse error: syntax error, unexpected '=', expecting '}' in ...... on line 69

It works perfectly if I look for another kind of character, eg, a period.
It seems PHP can't handle the backslash.

Thanks
mickeymouse
 
Posts: 36
Joined: 08. March 2009 06:10
Location: France & Canada
XAMPP Version: 5.6.3
Operating System: Win 7 Family Prem Ed (64 bit?)

Re: strrchr of a '\' doesn't work!

Postby Altrea » 15. March 2012 02:21

The Backslash has a very special meaning in PHP String functions, because it can terminate other special characters.
If you want to search for the Backslash itself you have to double the backslash as needle to terminate the char itself:

Second thing: if you don't want that PHP resolve special characters (e.g. \t, \r, \n) and variablenames in String functions, don't use double quotes around the parameter, but single quotes.
Code: Select all
$cntr = strrchr($myfilepath,'\\');


More about Strings in PHP for further reading: http://php.net/manual/de/language.types.string.php

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 6583
Joined: 17. August 2009 13:05
XAMPP Version: 5.5.19
Operating System: W7Ux64

Re: strrchr of a '\' doesn't work!

Postby mickeymouse » 16. March 2012 13:29

Thanks.
Works perfectly with "\\" & tks for the tip re single quotes.
mickeymouse
 
Posts: 36
Joined: 08. March 2009 06:10
Location: France & Canada
XAMPP Version: 5.6.3
Operating System: Win 7 Family Prem Ed (64 bit?)


Return to PHP

Who is online

Users browsing this forum: No registered users and 2 guests