"/" how to do this within php - Forward slahes

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
divx
Forum Newbie
Posts: 20
Joined: Sun Jul 01, 2007 1:37 pm

"/" how to do this within php - Forward slahes

Post by divx »

okay, its a simple question, but I have no idea how to search for this.

How can I put a forward slash within quotes.

I have a problem, a string gets converted to "xyz\" and is then put in an sql querry, how can I detect the slash, I cant use: strpos($encPass, "\" ) > -1
Since the \ makes the quotation a non surrounding quotaion.. so what can I use to detect a forward slash?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You escape it.

"\\"
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
phpBuddy
Forum Commoner
Posts: 37
Joined: Mon Nov 05, 2007 3:42 am

Post by phpBuddy »

Use substr() http://php.net/substr
to check last char of a string
and remove if backslash

The tricky thing is that '\' has to be escaped with one: '\'

Code: Select all

<?php

// a backslash has to be escaped by one: '\'
$string = 'xyz\\';
// see if last char is '\'
// -1 means last char. 1 means length = 1 char to test
if( substr( $string, -1, 1 ) == '\\')
    // From 0 first char, to length -1
    $string = substr( $string, 0, -1 );
echo $string;

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

rtrim() works better than substr() et al.
Post Reply