17
4

10 回答 10

44
于 2012-10-08T14:55:03.287 回答
4

You can have a mix of PHP and HTML in your PHP files... just do something like this...

<?php
$string = htmlentities("Résumé");
?>

<html>
<head></head>
<body>
<p><?= $string ?></p>
</body>
</html>

That should output Résumé just how you want it to.

If you don't have short tags enabled, replace the <?= $string ?> with <?php echo $string; ?>

于 2012-10-02T22:14:51.907 回答
4

In PHP there is a pretty good function utf8_encode() to solve this issue.

echo utf8_encode("Résumé");

//will output Résumé instead of R�sum�

Check the official PHP page.

于 2018-05-07T08:03:12.427 回答
1

So I try htmlspecialchars() or htmlentities() which outputs <p>Résumé<p> and the browser renders <p>Résumé<p>.

If you've got it working where it displays Résumé with <p></p> tags around it, then just don't convert the paragraph, only your string. Then the paragraph will be rendered as HTML and your string will be displayed within.

于 2012-10-02T22:11:43.520 回答
0

$str = "Is your name O\'vins?";

// Outputs: Is your name O'vins? echo stripslashes($str);

于 2013-04-26T08:57:40.007 回答
0

This works for me:

Create/edit .htaccess file with these lines:

AddDefaultCharset UTF-8
AddCharset UTF-8 .php

If you prefer create/edit php.ini:

default_charset = "utf-8"

Sources:

于 2015-12-27T06:55:38.750 回答
0

Try This

Input:

<!DOCTYPE html>
<html>
<body>

<?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars($str);
?>

<p>Converting &lt; and &gt; into entities are often used to prevent browsers from using it as an HTML element. <br />This can be especially useful to prevent code from running when users have access to display input on your homepage.</p>

</body>
</html>

Output:

This is some <b>bold</b> text.

Converting < and > into entities are often used to prevent browsers from using it as an HTML element. This can be especially useful to prevent code from running when users have access to display input on your homepage.
于 2016-09-07T13:29:56.190 回答
0

This works for me. Try this one before the start of HTML. I hope it will also work for you.

<?php header('Content-Type: text/html; charset=iso-8859-15'); ?>
<!DOCTYPE html>

<html lang="en-US">
<head>

于 2017-08-31T19:09:22.303 回答
0

The following worked for me when having a similar issue lately:

$str = iconv('iso-8859-15', 'utf-8', $str);
于 2018-06-26T21:00:09.897 回答
0

One of the best ways to do this is, change Collation in my SQL database.

step 1: Go to the Mysql database

step 2: Select the Text-based Row you want to get displayed (Eg., post or comments)

step 3: edit the row and select collation as below.

utf8mb4_unicode_ci

Make sure to change the collation of text rows whichever you want to display the special characters.

Sometimes htmlspecialchars_decode() or any other entity() doesn't convert your special chars to normal. So, the above method will definitely help.

于 2020-12-03T21:38:54.893 回答