/    Sign up×
Community /Pin to ProfileBookmark

Outputting special characters

I wrote a script that will output .csv data into a HTML table to then copy and paste into a website. My problem is that some of the characters are displaying as �. I tried running the data through the following function, but it doesn’t correct the problem.

`function filterData($data) {
$data = trim($data);
$data = str_replace(‘±’, ‘±’, $data);
$data = str_replace(‘°’, ‘°’, $data);
return $data;
}`

Does anyone have any suggestions on how to convert these characters into something readable?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 07.2021 — My first thought is to just apply htmlentities() to them:
[code=php]
echo htmlentities(trim($data));
[/code]
Copy linkTweet thisAlerts:
@NogDogJul 07.2021 — Oh, by the way, you may be able to fix it simply by specifying UTF-8 as the character set in your HTML:
[code=html]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
. . .
[/code]

If that works, you still may want to apply htmlspecialchars() (instead of htmlentities()) to the text if there's any risk of things like &lt; or &amp; characters being in the text. See https://php.net/htmlspecialchars for details.
Copy linkTweet thisAlerts:
@NtrimgsauthorJul 07.2021 — Thanks a lot! I think that did the trick.
Copy linkTweet thisAlerts:
@codewitchJul 07.2021 — There exists something called Ansi and something called Unicode.

https://en.wikipedia.org/wiki/Windows-1252

https://en.wikipedia.org/wiki/Unicode

According to what I know, one character in ANSI requires one byte, while one character in Unicode requires two bytes of memory. Hence there are much more Unicode characters than just ANSI characters.

Unicode also contains Greek, Latin, Cyrillic characters etc.
Copy linkTweet thisAlerts:
@NtrimgsauthorJul 27.2021 — @NogDog#1633862

I saw your reply late, but it solved the next problem I was having. Thanks!
×

Success!

Help @Ntrimgs spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...