tags:

views:

1432

answers:

2

In my webpage, I want the website to greet the user, but the username is surrounded by 'single quotations'. Since this isn't to prevent MySQL injection, i just want to remove quotes around my name on the display page.

Ex: Welcome 'user'! I'm trying to find the way where i can strip the quotations around the user and have it display on the example below.

Ex: Welcome user!

The only line of code that I can think relating is this:

$login = $_SESSION['login'];

Does anyone know how to strip single lines quotes?

A: 

I think the easiest way would be to use the trim() function. It usually trims whitespace characters, but you may pass it a string containing characters you want to be removed:

echo 'Welcome ' . trim($login, "'");

See http://php.net/trim

davil
+6  A: 

If you're sure that the first and last characters of $login are always a ' you can use substr() to do something like

$login = substr($_SESSION['login'], 1, -1); // example 1

You can strip all ' from the string with str_replace()

$login = str_replace("'", '', $_SESSION['login']); // example 2

Or you can use the trim() function, which is in fact the same as example 1:

$login = trim($_SESSION['login'], "'"); // example 3

My personal favorite is example 3, because it can easily be extended to strip away both quote types:

$login = trim($_SESSION['login'], "'\""); // example 4
Stefan Gehrig
Very thorough, good stuff! :)
abelito