How to remove unnecessary characters from string? - php

There is a problem in character when I fetch data from database.
doesn�t
It�s
I have tried to remove it using str_replace function but it's not working.
str_replace('�','',$str);
The encoding is
<meta charset="UTF-8" />
How can i solve these problem?

Try utf8_encode() function.
It will remove that characters
utf8_decode('doesn�t'); // doesnt
this is not good because it removes ' from doesn't
OR
Just update your Meta tag
<meta charset="UTF-8" />
to
<meta charset="ISO-8859-1" />
Output
doesn't
It's

no need to remove those character, Try to use htmlentities() or utfdecode() function to display those properly
This is because character encoding

Just removing the "�"s would give you wrong output. For instance, the string is probably doesn't, removing � from doesn�t gives you doesnt which is wrong.
What you should do is fix your encoding. You are probably saving or retrieving the strings in an encoding different from your code.

Related

Html Special Chars PHP

fm API to get event discription, venue name etc...
Now sometimes I get special chars back like: ' é à , but they show up scrabled.
So how can I display them properly? Also with the descrioption I get html-tags back, but I do want to keep these.
Can someone help me out fot those both cases? The language I'm using is php
Thanks in advance
specify encoding in the header:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
...
encode string when handling the input
$str=utf8_encode($str);
if you are displaying the input back as-is, no encoding is required;
however, if the value is the content of an input or textarea, escape the html characters
<?php echo htmlspecialchars($str); ?>
For Latin characters, use
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
in your section.
you need to be sure about two things, the meta header referring to which enconding you will be using, and the encoding you are using for the text served.
If you are using a utf8 header just be sure to convert the text served to utf8, refer to functions for encoding conversion like : mb_convert_encoding

Yii CHtml textField special characters in value

Code:
CHtml::textField('username', $username, array('class'=>'text'));
If my input field has Å type of special characters the value get disappeared. It solves if I put as htmlentities($username) in value field. But it prints the character as Å
I think it's because the values are get printed by going through CHtml::encode() function.
How should I print the correct value?. Any help please.
Make sure you have in the html head
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
If you work with multibyte strings, this may help:
mb_internal_encoding("UTF-8");
mb_regex_encoding('UTF-8');
Put the above 2 lines at the beginning of the code. I always include these at the first 2 lines of my index.php

PHP htmlspecialchars and character sets issue

I've got a page that loads content from a database. I have the page set to use utf-8 as such
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
I've set the collation on the db to utf8_unicode_ci, and I access the field from mysql using this:
$desc = htmlspecialchars($row['description'],ENT_QUOTES,'UTF-8');
I've done this to fix smart quotes and em-dashes. However, any field that contains those characters is being returned blank now. If I take away the 'UTF-8' parameter in my htmlspecialchars call I get the full text just with questionmark-in-a-diamond characters where the quotes should be. Is there something I'm missing?
If you're getting an empty string back, it probably means there's an invalid UTF-8 character in the content from the database. You could also set the ENT_IGNORE flag, but I'd recommend doing a binary search with the offending string to try to figure out exactly what the offending character is.
http://www.php.net/manual/en/function.htmlspecialchars.php. See the section under return value.

What is this character ( Â ) and how do I remove it with PHP?

It's a capital A with a ^ on top: Â
It is showing up in strings pulled from webpages. It shows up where there was previously an empty space in the original string on the original site. This is the actual character that is stored in my database. It's also what displays on my website when I echo a string that contains it.
I realize it's a character encoding problem when I originally process the webpage, but I am now stuck with these characters in my database. I have to convert this character when it is displayed, or somewhere else in the php before outputting html that contains it. I cannot reprocess the original documents.
I have tried str_replace() and html_entity_decode() and neither do anything.
What else should I try?
"Latin 1" is your problem here. There are approx 65256 UTF-8 characters available to a web page which you cannot store in a Latin-1 code page.
For your immediate problem you should be able to
$clean = str_replace(chr(194)," ",$dirty)
However I would switch your database to use utf-8 ASAP as the problem will almost certainly reoccur.
This works for me:
$string = "Sentence ‘not-critical’ and \n sorting ‘not-critical’ or this \r and some ‘not-critical’ more. ' ! -.";
$output = preg_replace('/[^(\x20-\x7F)\x0A\x0D]*/','', $string);
It isn't really one character, and is likely caused by misalignment between content encoding and browser's encoding. Try to set the encoding of your outputted page to what you are using.
e.g. In the section, output:
echo "<META http-equiv='Content-Type' content='text/html; charset=UTF-8'>";
(Adjust UTF-8 to whatever you are using)
I use this one a lot
function cleanStr($value){
$value = str_replace('Â', '', $value);
$value = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
return $value;
}
This is coming from database so the best option will be remove from database using a SQL query like:
UPDATE products SET description = REPLACE(description, 'Â', ' ');
Use Bellow codes
echo "<META http-equiv='Content-Type' content='text/html; charset=UTF-8'>";
echo htmlspecialchars_decode($your_string, ENT_QUOTES);
This problem occurs when using different charset in your web.
To solve this (using utf-8 in the examples):
in the <HEAD> of your page add charset:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
In any form you submit add accept-charset:
<form name="..." method=".." id=".." accept-charset="utf-8">
If you are using php+MySQLi to process your form, you should make sure the database connection is also supporting your charset. Procedural style:
mysqli_set_charset($link, "utf8");
and object oriented style:
$mysqli->set_charset("utf8")
I Actually had to have all of this:
<--!DOCTYPE html-->
<--html lang="en-US"-->
<--head-->
<--meta charset="utf-8"-->
<--meta http-equiv="X-UA-Compatible" content="IE=edge"-->
<--meta name="viewport" content="width=device-width, initial-scale=1"-->
<--meta http-equiv="Content-Type" content="text/html; charset=utf-8/" /-->
To remove â character from string
mysqli_set_charset($con,"utf8");
$price = "₹ 250.00";
$price2 = preg_replace('/[^(\x20-\x7F)]*/','', $price);
Result : 250.00

Convert html entities into charactes problem

link
Im having trouble converting the html entites into html characters, (&# 8217;) i have tried using al the different php functions (html_entity_decode, htmlspecial characters etc...) None seem to be working, any ideas what function i need to use?
Thank you!
Your problem isn't that the characters are not decoded correctly, but that the browser is misinterpreting the decoded characters.
As the page is encoded using UTF-8, you need to specify that in the header:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

Categories