I try to display the contents of a php file in a textarea with
<textarea ...>
<?php echo file_get_contents("file.php"); ?>
</textarea>
but it converts the html entities like ö to ö. As it is php code I can not use a conversion like htmlspecialchars because it would brick the <> and quotes etc.
EDIT:
I checked the sourcecode with the browser and there is really an ö.
Please help, thank you!
htmlspecialchars will change:
< ö
to:
< ö
and it's ok.
It will display in textarea:
< ö
After you submit form, it will send to server text
< ö
not:
< ö
Tested in Chrome with jQuery:
$('#layout').html('<form method="POST"><textarea name="x"><ö</textarea><input type="submit" /></form>')
I think there is some non-UTF-8 character in your file.php. Try this:
$content = file_get_contents('/tmp/file.php');
$content = mb_convert_encoding($content, 'UTF-8',mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
print_r($content);
You can refer this comment http://php.net/manual/en/function.file-get-contents.php#85008.
<textarea ...>
<?php echo htmlentities(file_get_contents("file.php")); ?>
</textarea>
Example
Convert some characters to HTML entities:
<?php
$str = 'Go to w3schools.com';
echo htmlentities($str);
?>
The HTML output of the code above will be (View Source):
<a href="https://www.w3schools.com">Go to w3schools.com</a>
The browser output of the code above will be:
Go to w3schools.com
Related
I'm building a sign-in/up form and I have problems displaying HTML characters. When a user signs up, I use this function for the sign-up data and then insert it into the database.
function clearInput( $string) {
$string = stripslashes($string);
return htmlentities($string);
}
When a user signs-up with the name <p>hello</p> it will look like this in the db: "<p>hello</p>"<br>.
If the user signs-in and I var_dump the name that is saved in the session it looks like this <br>'<p>hello</p>' in the browser.
If I echo this <p>hello</p> manually in the document, it displays this <p>hello\</p> as it should normally.
Does someone know how it shows <p>hello</p> when I var_dump the session name?
I don't get your question but it will help you.
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars_decode($str);
This is some <b>bold</b> text.
$str = '<a href="https://www.test.com">test.com</a>';
echo html_entity_decode($str);
test.com
I am trying to take the rss/xml feed from itunes and I have noticed that artist and songs that have special charters like é like in beyoncé is showing as Beyoncé
I have tried the following to get it to show correctly but unsucessfully I have Googled searched and searched on here for the correct answer but sadly not working.
here is what I have tried - I maybe way off.
echo html_entity_decode($entry->imartist, ENT_COMPAT, 'UTF-8');
here is the full code
function itunes(){
$itunes_feed = "https://itunes.apple.com/au/rss/topsongs/limit=100/explicit=true/xml";
$itunes_feed = file_get_contents($itunes_feed);
$itunes_feed = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $itunes_feed);
$itunes_xml = new SimpleXMLElement($itunes_feed);
$itunes_entry = $itunes_xml->entry;
foreach($itunes_entry as $entry){
echo html_entity_decode($entry->title."<br>", ENT_COMPAT, 'UTF-8');
echo html_entity_decode($entry->imartist, ENT_COMPAT, 'UTF-8');
echo "<br><br>";
// Get the value of the entry ID, by using the 'im' namespace within the <id> attribute
$entry_id['im'] = $entry->id->attributes('im', TRUE);
echo (string)$entry_id['im']['artist'];
//echo $entry_id['artist']."<br>";
}
}
That feed is in valid UTF-8, you shouldn't need to decode it with html_entity_decode. What happens if you add a <meta charset="utf-8" /> in the <head> of HTML page ?
So here's the problem. I have data in a MySQL DB as text. The data is inserted via mysql_real_escape_string. I have no problem with the data being displayed to the user.
At some point I want to pass this data into a javascript function called foo.
// This is a PHP block of code
// $someText is text retrieved from the database
echo "<img src=someimage.gif onclick=\"foo('{$someText}')\">";
If the data in $someText has line breaks in it like:
Line 1
Line 2
Line 3
The javascript breaks because the html output is
<img src=someimage.gif onclick="foo('line1
line2
line3')">
So the question is, how can I pass $someText to my javascript foo function while preserving line breaks and carriage returns but not breaking the code?
===========================================================================================
After using json like this:
echo "<img src=someimage.gif onclick=\"foo($newData)\">";
It is outputting HTML like this:
onclick="foo("line 1<br \/>\r\nline 2");">
Which displays the image followed by \r\nline 2");">
json_encode() is the way to go:
$json = json_encode($someText); # this creates valid JS
$safe = HtmlSpecialChars($json); # this allows it to be used in an HTML attribute
echo "<img src=someimage.gif onclick=\"foo($safe)\">";
You can see a demo here: http://codepad.org/TK45YErZ
If I'm not interpreting badly you may do this:
// This is a PHP block of code
// $someText is text retrieved from the database
echo "<img src=someimage.gif onclick=\"foo('{".trim( preg_replace( '/\s+/', ' ',$someText ) )."}')\">";
You'll save yourself a lot of headaches by pulling the JavaScript out of the HTML:
<img id="myImage" src="someimage.gif"/>
<script type="text/javascript">
var str = <?php echo json_encode($json); ?>;
document.getElementById('myImage').addEventListener(
'click',
function() {
foo(str);
}
);
</script>
Or something similer...
Only json_encode() is enough to escape the new line
echo "<img src=someimage.gif onclick=\"foo(".json_encode($newData).")\">";
i have a textarea value which its value derived from a field(nl2br)
how to strip off "< br/>", so that when i want to edit this field, the "< br />" will not be appeared?
//$data["Content"] is the field that has <br/> tags inside
$content = $data["Content"];
//when want to edit, want to strip the <br/> tag
<td><textarea name="content" rows="10" style="width:300px;"><?=$content?></textarea></td>
i know it should be using strip_tags() function but not sure the real way to do it
any help would be appreciated
If you wanna use strip_tags, then it would just be:
$content = strip_tags($data["Content"]);
i would be using str_replace the following will replace <br/> with newline
$content = str_replace('<br/>','\n',$data['Content']);
or if you don't want the newline
$content = str_replace('<br/>','',$data['Content']);
edit
an example
$my_br = 'hello<br/> world';
$content = str_replace('<br/>','',$my_br);
echo $content;
Output: hello world
When I try to display Vietnamese characters with the following code:
<?php
$str = "Nghệ thuật cắm hoa vải";
//echo utf8_encode(html_entity_decode(($str)));
echo html_entity_decode($str);
//echo $str;
?>
I get Ngh�? thu�?t c??m hoa va?i as a result.
Tried several option but couldn't make it. Any ideas?
Is the PHP script encoded in UTF-8? If it is, send a header indicating so:
header("Content-type: text/html; charset=utf-8");
Alternatively, do:
echo mb_convert_encoding($string, "HTML-ENTITIES", "UTF-8");
Works fine for me: http://codepad.org/uTmORRmz
Does your browser support Unicode?