how to display double quote instead of &34; from php variable - javascript

I am pulling my hair with this simple task...
From a php variable that contains double quotes (") which is echoed in a div, the html source shows that " have been replaced by ". So far OK.
However, when the html code within the div is copied with jQuery, some of the double quotes are repeated.
I need to fix that in order to use MindMup editor properly.
$Text = 'This is an <span class="myclass">example</span> where double-quotes are added by "I don\'t know what".'
<a onclick="javascript:copyEditor();">copy code</a>
<div id="editor">
echo $Text;
</div>
<script>
function copyEditor() {
$('#hiddenEditor').html($('#editor').html());
}
</script>
If the text is typed directly into the html page with double quotes, then it is fine. See the difference in that example.
So my question is how do I stop php/html to convert the double quotes into " when displaying that variable in the page?
There is also an empty line added on top in the link above after copying the code... why?

Replace double quotes with "
To replace the quotes in user input:
$escaped_quotes = str_replace( "\"", """, $string );
source

Related

Javascript not working inside a PHP function

I'm new to javascript.
There seems to be no result but a blank screen when I try the following code:
function send_to_class($data)
{
echo 'PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP';
?>
<script type="text/javascript">
$(".username").append(<?php echo $data;?>);
</script>
<?php
echo 'EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE';
}
and my PHP
$data = "You might think I'm Bonkers, but I just think I'm Free";
send_to_class($data);
and my html
<span class="username"></span>
The Function is declared before the send_to_class($data) is initiated.
The P's and the E's are showing up just fine, there's just no middle sentence.
I've tried divs, id's and classes, adding css and changing the script tag.
2 things, a) how do I get the function to work which appends $data to the .username class and b) how do I get it to work if $data is an array? Can Javascript print php arrays in this manner?
Thanks.
I found that because the $data I was sending contained "'s that it needed to have the "'s escaped. So in my php file that was producing the data, I had to turn the following:
<div class="class1">some words</div>
into this:
<div class=\"class1\">some words</div>
This is because it's javascript that's doing the printing, and when javascript does printing of "'s it needs to have them escaped with a backslash like this \".

Find and replace <?php & ?> in string

I have a large string of valid HTML code that will be inserted into the DOM, the string also contains PHP code. I want the php code to be displayed as plain text, Chrome automatically comments out the PHP code. Obviously the PHP code is wrapped in <?php ... ?> so my question is:
How can I replace the open <?php with a <span> tag, and replace the ?> with a </span> to close the open <span> tag so that the PHP code is within the <span> tags and therefor visible as plain text?
EDIT: I will be wrapping the PHP code back in <?php ?> by replacing the span, so escaping the characters will make that more difficult.
You could escape it.
From:
https://css-tricks.com/snippets/javascript/htmlentities-for-javascript/
function htmlEntities(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
As you say that the HTML is a string, you can easily just use the replace() function.
var html = // your html
var sanitized = html.replace(/<\?php/g, "<span>").replace(/\?>/g, "</span>");
Use Regular Expressions!
RegExp101 is a great playground.
Remember to change the flavor on the left menu to JavaScript.
The easiest way is using the .replace() function.
var str = str.replace(/(<\?php)|(<\?)|(<\?=)/g, '<span>').replace(/(\?>)/g, '</span>');
Edit: As requested by Get Off My Lawn, I've added support to <?= nd <? tags. The pipe | represents an or in Regular Expressions.

how to escape multiple hierarchy quotes in string (HTML)

I am working on application with java as backend and Angularjs 1.0 for Front end. I am using veasy Angularjs plugin to show datatable. Now I have managed and tweaked veasy plugin to customize a lot for my app.
Now I am stuck on silly thing, I know how to escape an quote by prefixing "\". But problem here is as I am using multiple hierarchy of quotes both single and double and I am not able to add another quote without breaking the HTML.
Flow of the code:
That is a Json that will pass to veasy table. one of the key in that JSON will have HTMl as its value, I have already compiled it in Angularjs, & Its working.
code has a div with bootstrap popover on focus and it also has an attribute that contains html for that popover, now html content has many li for which I need to add ng-click (to each li) with function that passes string value
what I need is to pass a value (string) with that function (changevehicle()) but I am not able to add quotes in it, as soon I add a quote there, HTML is broken.
I have tried doing this and a lot: changevehicle(\'string\'), I have tried with single quotes double quotes with and without esaping.
I guess my question is not clear. the below code is working, but when I add string value to that function as an argument (changevehicle()), HTML is broken.
{[vehicle : "<div class='curp use-ellipsis' data-trigger='focus' tabindex='0' role='button' jobplanningpopover popover-html4='<ul class=\"list-unstyled\"><li class=\"emph curp\" ng-click=\" changevehicle() \">xxxxxY 2P<\/li><li class=\"curp\">CHIILL 6P<\/li><li class=\"curp\">NKL 235<\/li><li class=\"curp\">KHD 654<\/li><li class=\"curp\">YET 874<\/li><\/ul>' popover-placement4='bottom' > {{ xxxxxvehicle }} <\/div>"]}
Using " in the place of escaped quotes should do the trick. HTML recognizes this as an escaped quotation mark.
"<div class='curp use-ellipsis' data-trigger='focus' tabindex='0' role='button' jobplanningpopover popover-html4='<ul class="list-unstyled""><li class="emph curp" ng-click=" changevehicle() ">xxxxxY 2P<\/li><li class="curp$quot;>CHIILL 6P<\/li><li class="curp">NKL 235<\/li><li class="curp">KHD 654<\/li><li class="curp">YET 874<\/li><\/ul>' popover-placement4='bottom' > {{ xxxxxvehicle }} <\/div>"]
sometime you just need fresh eyes for a problem , today I solved it in just few minutes
below code worked for me
{[vehicle : "<div class='curp use-ellipsis' data-trigger='focus' tabindex='0' role='button' jobplanningpopover popover-html4='<ul class=\"list-unstyled\"><li class=\"emph curp\" ng-click=\" changevehicle("+"'_string_'"+") \">xxxxxY 2P<\/li><li class=\"curp\">CHIILL 6P<\/li><li class=\"curp\">NKL 235<\/li><li class=\"curp\">KHD 654<\/li><li class=\"curp\">YET 874<\/li><\/ul>' popover-placement4='bottom' > {{ xxxxxvehicle }} <\/div>"]}

having trouble in displaying text (with double quotes stored in a variable) to a modal through javascript

I am having trouble in displaying text with double quotes through javascript. I want to display a text with double quotes in a modal. But in order to do this, I have to use javascript. The code is like this:
HTML code:
<button id="info" class="btn btn-link" data-toggle="modal" data-target="#infoDetails" data-name="<?php echo $name;?>" data-detail="<?php echo $details;?>" >Click Here</button>
HTML code (inside modal):
<p align="justify" id="detaiLInfo" style="font-size: 14px"> </p>
javascript code to display modal:
<script>
$('#infoDetails').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var name = button.data('name') // Extract info from data-* attributes
var details = button.data('detail')
var modal = $(this)
modal.find('.modal-header #name').text(name)
modal.find('.modal-body #detaiLInfo').text(details)
});
Everything's working properly BUT when the value of $details ="The term biology is derived from the Greek word βίος, bios, \"life\" and the suffix -λογία, -logia, \"study of.\"";
It only gives this OUTPUT (inside the modal):
The term biology is derived from the Greek word βίος, bios,
But when I try to echo it inside html code (but not inside the code for modal) like this: echo $details; , the result is:
The term biology is derived from the Greek word βίος, bios, "life" and the suffix -λογία, -logia, "study of."
Can anyone help me to solve this? Please..
If you examine what's happening, the HTML code generated by the server, as seen literally by the browser client, will get the " that opens "life" and be interpreted as the closing quote of the data-detail attribute, followed by trailing garbage which is then forgiven and ignored by the browser's HTML parser. I suggest using UTF-8 curly quotes in your explanatory text, side-stepping the issue and looking nicer! Otherwise, you could entity-encode the offending internal quotes. In general, packing long and/or formatted text into data fields is sort of a mis-use of the idea. Perhaps such data could instead be packed into the HTML content of a carefully id'd display: none storage area to be extracted dynamically with JS.
Scanning again it looks like a capp-o in detaiLInfo

Apostrophes issu with php and jscript

I have this piece of code, which doesn't work, I couldn't find where is the problem?
$texttt = "blabla";
echo "<div onclick='select(\" page d \' accueil \");'>".$texttt."</div><br/>";
function select(text){
alert(text);
}
" is breaking your php statement
echo "<div onclick=\"select(' page d\' accueil ');\">\"Page d'accueil\"</div><br/>";
function select(text){
alert(text);
}
try to use addslashes and htmlentities to escape the quotes between text
$texttt = htmlentities(addslashes($text), ENT_QUOTES);
echo "<div onclick='select(\"$texttt\");'>$texttt</div><br/>";
Use single quotes for PHP. I use single quotes for PHP (which is server side script) and double quotes for client side scripts unless it is not obligatory.
echo '<div onclick="select(\'page d\'accueil\');">"Page d\'accueil"</div><br/>';
the problem is sourced by single quote in d'accueil:
I dont know if it is possible to escape a string which is located in another escaped string.
The possibilities:
You can change the grammar rules of your language:
Affects too many people.
Not applicable.
You can change the way of typing single quote: using ' in context.
I have typed a new code (which is working well) for you :
<?php
echo '<div onclick="select(this.innerHTML);">"Page d'accueil"</div><br/>';
?>
<script>
function select(text){
alert(text);
}
</script>
you should avoid useless quotes before you get lost inside.
"Page d'accueil" don't need to be between quotes so remove them.
Use one kind (single or double quotes) for the first level of quoting, and the other for the next levels, and keep that it mind.
<? php echo '<div onclick="select(\" page d\' accueil \");">Page d\'accueil</div><br/>'; ?>
<script type="text/javascript">
function select(text){
alert(text);
}
</script>
you can use heredoc syntax
refreence : http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
also try this:
echo json_encode("<div onclick='select(" page d ' accueil ");'>{$texttt}</div><br/>");

Categories

Resources