JK36
JK36

Reputation: 853

Cant get the substr to recognise the \ character

Some of my users log into the system using the domain name, so if the domain is "theHub", the type in thehub\j51289 as their username.

The username ends in a session variable called username, however I need to remove the domain name from the username if it exists.

I've found substr function to work up until the \ character - and then it fails. I know that it is an escape character, but can't figure out how to implement it in my code, can anyone help?

    if(substr(strtoupper($_SESSION['username']), 0, 7 ) === "THEHUB\") 
       { echo "it has found it"; 
         $_SESSION['username'] = str_replace(strtoupper("THEHUB\"", "", $_SESSION['username']));    
       } 
    else 
       { echo "no it has not found it";}

Upvotes: 0

Views: 49

Answers (4)

rap-2-h
rap-2-h

Reputation: 32028

You should escape backslash \ with another backslash : \\ like this : "THEHUB\\"

(You can have a look here : http://php.net/manual/en/language.types.string.php)

Upvotes: 0

Jonnny
Jonnny

Reputation: 5039

\ Will escape the " \\ will escape the \

Upvotes: 0

salrawaf
salrawaf

Reputation: 349

Use addslashs to escape the string. https://www.php.net/addslashes

Upvotes: 0

cdhowie
cdhowie

Reputation: 169143

You need to escape the backslash with another backslash. For example:

echo "foo\\bar";

Will display the following:

foo\bar

In this case it sounds like you want to use "THEHUB\\".

Upvotes: 2

Related Questions