php quotation strip
Solution 1
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
Solution 2
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, "'");
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
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?
-
Xitcod13 almost 12 yearsis substr() method the fastest when it comes to performance??
-
Stefan Gehrig almost 12 years@Xitcod13: How often do you expect that code to run? If you're not talking about several thousand calls: don't worry!
-
thechriskelley almost 10 yearsKeeping the biennial commenting going: @ Xitcod13: substr: 3.1371190547943s, str_replace: 4.3370628356934s, trim: 2.6876559257507s. Trimmed a string $strwithquotes="'this is the login string'" 10000000 times with each function above :) - PHP 5.4.24 (cli) (built: Jan 19 2014 21:32:15)