I have a variable $teste = hiiiii
I need to put the variable inside a quote and a double quote.
Like this:
"'$teste'" for the value be this "'hiiiii'"
I'm trying concatenation but not working. How do that???
You can escape your quotes:
echo "\"'$teste'\"";
will print out: "'hiiiii'"
You can do it like this:
$result = '"\'' . $teste . '\'"';
this will result in: "'hiiiii'".
Demo
Try before buy
Using basic cheat of single and double quote
$test = 'hi';
echo '"'."'".$test."'".'"';
echo "'".'"'.$test.'"'."'";
Related
I am using the following code for adding the singles quotes for a string
$gpids=implode("','",array_unique($groupIds));
My output coming like these
156','155','161','151','162','163
I want my output like these
'156','155','161','151','162','163'
please help me
Using concatenation operator
$gpids = "'".implode("','",array_unique($groupIds))."'";
Just concate quote :
<?php
$gpids="'" . implode("','",array_unique($groupIds)) . "'";
echo $gpids;
?>
You have two options:
Simple one:
$string = "'" . implode("','",array_unique($groupIds)) . "'";
Second one:
function add_quotes($str) {
return sprintf("'%s'", $str);
}
$string = implode(',', array_map('add_quotes', array_unique($groupIds)));
Try this
$query=$key.'='."'$value'".',';
Here $value will have single quotes.
I have a variable that I echo, like this:
echo "hm={$yes["n"]}";
I need to replace every instance of (whitespace) with +. What I tried was putting this:
str_replace(" ","+",{$yes["n"]});
before I echoed it out.
It said unexepected {, so I tried:
str_replace(" ","+",$yes["n"]);
Where nothing happened.
You have forgotten to assign the output of str_replace to the variable.
$yes["n"] = 'string with whitespace';
$yes["n"] = str_replace(" ","+",$yes["n"]);
echo "hm={$yes["n"]}"; // echoes hm=string+with+whitespace
Is this the right way to escape a string just in case or I can insert string like this without additional escaping?
$filenamefordb = preg_replace('/[^A-Za-z0-9а-яА-Я_\.\-]/u', '', $filenamefordb);
$query = "INSERT INTO file SET filename='$filenamefordb";
I don't use mysqli_escape because I also need name without any quotes in another place
Why don't you escape the string using PDO?
<?php
$conn = new PDO('sqlite:/home/lynn/music.sql3');
/* Complex string */
$string = "Co'mpl''ex \"st'\"ring";
print "Unquoted string: $string\n";
print "Quoted string: " . $conn->quote($string) . "\n";
?>
This will output
Unquoted string: Co'mpl''ex "st'"ring
Quoted string: 'Co''mpl''''ex "st''"ring'
Reference:
http://php.net/manual/it/pdo.quote.php
you can escape it with a generic php function:
$filenamefordb = mysql_escape_string ($filenamefordb);
$query = "INSERT INTO file SET filename='$filenamefordb";
I want to get date using date function but the date should comes between " ". I want to assign " to a variable so that i can get my output by combining the two variables:
$td=""";
$td2="".date("m/d/Y")."";
$td3=""";
$date="$td"."$td2"."$td3";
Please help...?
If you don't want to escape them, just wrap them in a single quote instead:
$td = '"';
$td2 = '"'.date("m/d/Y").'"';
$td3 = '"';
$date = $td.$td2.$td3;
You need to escape them
$td = "\"";
With the backslash, the character is treated as a character by any means necessary and is ignored by php, it wont be used to limiting strings or someting like that.
Try
$td=""";
OR
$td = "\"";
Use backslash or single quotes .
You have to use "\" before " in your $td and $td3 variable.
$td="\"";
Basically I have a block of html that I want to echo to the page and the html has the $ sign in it and the php thinks it is a variable so $1 is treated as the variable not the value and is not displayed.
There is the standard answers here but none are working: PHP: How to get $ to print using echo
My next idea is to split the string at the $ and echo each part out.
Here is the code I have tried echo and print.
foreach ($rows as $rowmk) {
$s = $rowmk->longdescription;
//$s = str_replace('$', '#', $s);
$s = str_replace('$', '\$', $s);
//echo "$s" . "<br>";
print $s;
}
All help appreciated.
OK I solved by using the character code value for $
foreach ($rows as $rowmk) {
$s = $rowmk->longdescription;
$s = str_replace('$', '$', $s);
echo $s . "<br>";
}
I figured I should just post it anyway.
Thanks,
Mat
Or you could echo string literal using single quotes...
<?php
echo 'Give me $1';
?>
will print:
Give me $1
PHP string docs:
http://php.net/manual/en/language.types.string.php
Side note - the link you provide has many answers that would work perfectly. How are you applying them in a way that doesn't work?
Just use a single quoted string.
$foo = 'Hello';
echo '$foo'; // $foo
echo "$foo"; // Hello
You're doing it in the wrong place. Variable interpolating is done when double quoted string literal (which in your case is stored within $rowmk->longdescription is daclared. Once it's done, you can't really do anything to get your $s back.
Solution, do proper escaping, when you declare the string.
I assume you read your rows from a database. Dollar Signs inside these strings will not be interpolated by php. Here's a little test script to try it out:
// you'd first have to set the three variables according to your database
$dbh = new PDO($DSN, $DB_USER, $DB_PASS);
// create a table and insert a string containing a dollar sign
$dbh->exec('CREATE TABLE IF NOT EXISTS some_text ( longdescription VARCHAR( 255 ))');
$dbh->exec('INSERT INTO some_text ( longdescription ) VALUES ( "10 $" )');
// query all the data from the table
$query =$dbh->query("SELECT * FROM some_text");
$rows = $query->fetchAll(PDO::FETCH_CLASS);
// loop over all the rows (as in your example) and output the rows
// no problem at all
foreach ($rows as $rowmk) {
$s = $rowmk->longdescription;
echo $s . "<br>";
}
You can use "\$"
ex:
"\$stringvalue"
I did it using this
echo "$" . "VariableName";