Create variable with apostrophe and double quote PHP - php

How to create variable when you have massive data with ' and " in it whitout addslashes.
<?
// Create variable with single and double quote.
s = ' massive data with """ and '''' inside.... ';
?>
I try
addslashes(s) = ' massive data in with """ and '''' inside.... ';
but i think i miss something. I cant change manually every apostrophe because its massive.
Thanks you !

You're looking for the backslash escape character:
$string = ' massive data in with """ and \'\'\'\' inside....';
Simply use a \ before any character you wish to be interpreted as part of a string.
I've created an 3v4l demonstrating the above code here.
Alertnatively, if you have a large text string that contains both single quotes and double quotes, you can use a heredoc to avoid needing to escape individual characters:
$str = <<<EOD
' massive data with """ and '''' inside.... ';
EOD;
echo $str;
I've created another 3v4l example of this here.
Hope this helps! :)

Related

HTML quotes inside of quotes inside of quotes

So I am writing HTML and I know if I need to put quotes inside other quotes I can use "" and '' but what if I need more than 2 sets. quotes inside quotes inside quotes. Are there more quote types I can use?
Quotes only have special meaning in HTML when you need to delimit attribute values.
If you need to represent a quote character inside an attribute value delimited with quote characters, use a character reference: ".
Nested quotes have no significance, so you can keep using " (although it doesn't make much sense in English to do do).
You put the php tag so I guess you have troubles inside your php code.
Here is an example in PHP :
$var = "this is a sentance : \"test sentence with ' which is a quote\" END ";
you will have this is a sentance : "test sentence with ' which is a quote" END in $var

How do I echo the original HTML in PHP?

I have some HTML. In my HTML, there is a form that uses a PHP script. When I submit this form, it sends me to a blank page. I have tried this, as suggested in a similar question, but it doesn't work because I have HTML events that call functions with arguments, so both " and ' are already used... I am new to PHP, so, is a there a solution to this that I do not know of?
Use the backslash character \ to escape your quotes like so \". The backslash causes PHP to treat the quote as a regular character. :]
Example:
<?php echo "Hello World"; ?>
outputs Hello World
<?php echo "\"Hello World\""; ?> outputs "Hello World"
Escape your quotes.
echo "<h3>First</h3>"
\" is an escaped quote. When a quote is escaped, the environment treats it as an ordinary character, not as a quote. \' Single quotes can also be escaped if need be.
(Not the best example since I'm not also using singles, but you get the point.)

using html and java script tags in php

I am confused about using single and double quotes and back slash while using java script and html tags in php can any one please clarify i googled it but still not clear about it. i am confused for this small thing.i am new to programming
- here is my code
<?php
if(isset($_GET['id'])) {
echo '<div id="d2">';
include "test2.php";
echo '</div>'; }
else
{
echo '<div id="d1">';
include "insert.php";
print "<script type=javascript>"
print "document.getEelementById('alertdiv1').innerHTML='hi' ;"
print "</script>"
echo '</div>';
}
?>
In PHP, you can enclose a string in either single quotes or double quotes. Both are valid:
$var = "this is a string";
$var2 = 'this is also a string';
The main difference is that if your string contains a variable, and you want the variable content to be treated as part of the string, you need to use double quotes:
echo "$var which I made";
will return:
this is a string which I made
When you are manipulating html, css and JavaScript strings, you need to make sure that you don't accidentally close your PHP string. For example:
echo "<h1 class='myheading'>Heading Text</h1>";
Notice how I used double quotes to enclose my string? Because I did that, I was able to use single quotes in the html, without escaping them.
If I'd wanted to use double quotes in my string, I would have had to escape them, like this:
echo "<h1 class=\"myheading\">Heading Text</h1>";
The \ tells PHP that the double quote which immediately follows is to be treated as a literal, and not used to terminate the string.
I can't see any problems relating to quotes in your code.
<script type=javascript> — That is not a valid value of the type attribute (which is optional anyway now). Get rid of the type attribute.
document.getEelementById — Element only has 3 es in it, not 4.
alertdiv1 — There is no element with that id in your code
hi as far as concerned single quotes and double quotes doesnt matters when its a string.but when you use any variable inside
$a = 'hi';
echo '$a' ;
will output
$a
but when you use " "
$a = 'hi';
echo "$a" ;
it will print
hi
Basically, if you're using "" (quotation marks) as your delimiter and you then use a quotation mark as part of the string you must escape it by putting a backslash in front of it.
For example:
$string = "This is my string"; // This is fine as the value of the string doesn't contain any quotation marks (")
$string = "I am including a quote in my string \"To Be Or Not To Be\"."; // This is also fine, as I have escaped the quotation marks inside the string
The same applies if you're using '' (apostrophes) as your delimiter and you then want to use them as part of the string, you must escape them using back slash ().
Hope that helps.
$var = "AAA";
echo 'This costs a lot of $var.'; // This costs a lot of $s.
echo "This costs a lot of $var."; // This costs a lot of AAA.

PHP variables and strings

Getting very confused with echoing an HTML anchor with a variable inside.
<?php
echo ' Next';
?>
I've tried so many variations of lost which ones I've tried. One of the attempts was with curly brackets { but still nothing. I know I'm getting my single and double quotes muddled up!
Could somebody please put me straight on this one. Also, what is the rules for apostrophes and quotes in PHP. If I want to echo something, what shall I start it with, an apostrophe or a quote.
<?php
echo ' Next';
?>
If you want to do some math of other trickery inside an echo, you will need to surround it in brackets.
Edit: #DaveRandom points out that the exception to the trickery clause is $var++ and ++$var.
If you use ' when printing string, everything inside is treated as a text.
If you use ", variables passed inside are converted to the their values.
However it's impossible to do a math operations inside ". You have to escape it and do it in 'PHP way'.
<?php
echo ' Next';
?>
Use double quotes "something" and surround the variables with curly brackets when they are inside the quotes.
echo " <a href='?p={$current_page+1}'>Next</a>";
You can also use string concatenation, which basically means joining a few strings together:
echo 'something' . 'something else' . $my_variable;
As for escaping, if anywhere inside some quotes you want to insert a quote of the same type (e.g. if you surround your script with double quotes and you want to insert a double quote), you need to escape these quotes by prepending them with a backslash - \.
For example you want to output Text and you have surrounded it in double quotes, you need to escape these double quotes in the HREF attribute by prepending them with a backslash \, so the result should be Text.
The following are valid ways of escaping and displaying characters:
echo "it\" so nice to be here";
echo 'it\'s so nice to be here';
echo "it's so nice to be here"; // Different quotes, no need to escape
echo 'it"s so nice to be here'; // Different quotes, no need to escape
The following will result in an error:
echo 'it's so nice the be here';
Because the PHP interpreter will assume the expression to be ended with the quote found in it's, resulting in the rest of the line being treated is invalid code.
For more information you can read the PHP documentation on the echo() function and this wonderful article on Quotes and Strings as well.
I assume you want to do this:
echo ' Next';
You can try This
$link = ' %s';
printf($link, $current_page - 1, "Prev");
printf($link, $current_page + 1, "Next");

PHP Both kinds of quotes in variables

I used to know this, but I guess it slipped my mind.
What's the code you put in the beginning and the end of a variable in php to allow both kind of quotes?
The personal rules I follow (not a silver bullet):
If there is no single quotes in the string - use '
If there is single quote(s) in the string - use "
If there are different types of quotes in the string - use ' or HEREDOC
I think you are thinking of heredocs.
Example:
echo <<<EOT
Some text here '' ""'
This should print a capital 'A': \x41
EOT;

Categories