I'm trying to get usernames from this website and this is what I've done:
$div = $html->find('div[class=micro-home-recent-review review-item]');
for ($i=0; $i<count($div); $i++){
$username = $div[$i]->find('div[class=tooltip-fullname]', 0)->find('b', 0)->plaintext;
// I've tried using iconv but apparently it doesn't work
$username = iconv(mb_detect_encoding($username), "UTF-8", $username);
$query = "INSERT INTO users ('name') VALUES ($username)";
$pdo->query($query);
}
Then the newly inserted records in my database are:
As you can see, most of the names are recorded with HTML symbols, which can be displayed normally on browsers, but get messed up when shown as JSON. The same problem happens when I tried to get reviews, and below is the sample JSON of a review:
I need the JSON to show data in my Android app, therefore this problem needs to be solved or the data won't be displayed properly. What could be a possible solution for this? I really need your help and suggestions.
try to use html_entity_decode() function.
use htmlentities_decode() that will solve your problem.
Related
I have a PHP project that allows users to submit an article / tutorial and before I insert the data to my database I do
$content1 = htmlspecialchars($userscontent);
$content = htmlentities($content1, ENT_QUOTES);
for safety purposes and when I output the data from my database I decode it. Now I want that text to be structured and not just written on one line and I also want to add the ability to add images to the articles and I have no idea which is the best way to go about this.
Any help is appreciated.
Could you not just use the decode?
$result1 = html_entity_decode($result, ENT_QUOTES);
$result = htmlspecialchars_decode($result1);
References:
Html entity decode
Htmlspecialchars decode
Although, I would definitely recommend doing what #CD001 says and use the HtmlPurifier library.
I try to get name and adress and other data from database which the name and adress atributes are in text format, while the other atributes are in varchar format. I don't understand why some of those name and adress can not be caught while they are there in the db. From about 5000 records there are about 300 records which the name and adress can not be caught in my program. What I got after research is several of the names and addresses started with white space. So after I removed those white spaces, it works just fine. But for the others I just don't understand. They seem just normal text. Here the codes:
$hasil_query1= json_encode($data->getData($query));
$obj1 = json_decode($hasil_query1,true);
$name=$obj1[0]['name'];
$address=$obj1[0]['address'];
When I print $name or $adress, some of them don't appear (not caught). But when I use this code, instead of the above, it works just fine
$result = mssql_query($query);
while($row = mssql_fetch_array($result))
{
$name=$row['name'];
$address=$row['address'];
}
Please help me understand this because I just can not see it untill now.
Thanks for your advance.
Try this
$hasil_query_baru = array();
$hasil_query1= $data->getData($query);
foreach($hasil_query1 as $row){
$hasil_query_baru[] = array_value($row);
}
$hasil_query1 = json_encode($hasil_query_baru);
...
A var_dump may help understand the problem.
var_dump($data->getData($query));
I found the answer from php json_encode() show's null instead of text
"json_encode expects strings in the data to be encoded as UTF-8.
Convert them to UTF-8 if they aren't already"
~phihag
Thanks to Surgeon of Death who gave me that link.
I generate json trough php, when I get the data in json, I get it in a strange way, sometimes with 2 question marks, like in example:
{"markers":[
{"latitude":"48.6916858",
"longitude":"13.9314643",
"content":"Ort Kn\u00f6belsteiner??<\/b>28.12.2014. - 12:00"}}]}
Here is the code that generates the json:
while ($row = mysqli_fetch_array($fetch, MYSQLI_ASSOC)) {
$row_array['latitude'] = $row['latitude'];
$row_array['longitude'] = $row['longitude'];
$row_array['content'] = utf8_encode($row['grcnt']);
array_push($return_arr,$row_array);
}
echo '{"markers":'.json_encode($return_arr).'}';
I am not sure why am I getting this ?? signs. I tried to google it but no answer. I used json with sql queries combined with html tags, since I need to show this info on a mark (point) on the map. Sql result after query is shown normal, without this question marks. Any help would be appreciated since I am not really sure why is this and in which case it happens. Thank you
SOLVED IT! :)
the problem was in utf8_encode() line, I removed it and made a new query after the connection to the db, wrote this:
mysqli_query ( $link ,"SET NAMES 'utf8'" );
You use json_encode? Php and javascript have diferent ways to see objects. Look at the second parameter of json_encode
I'm storing HTML and text data in my database table in its raw form - however I am having a slight problem in getting it to output correctly. Here is some sample data stored in the table AS IS:
<p>Professional Freelance PHP & MySQL developer based in Manchester.
<br />Providing an unbeatable service at a competitive price.</p>
To output this data I do:
echo $row['details'];
And this outputs the data correctly, however when I do a W3C validator check it says:
character "&" is the first character of a delimiter but occurred as data
So I tried using htmlemtities and htmlspecialchars but this just causes the HMTL tags to output on the page.
What is the correct way of doing this?
Use & instead of &.
What you want to do is use the php function htmlentities()...
It will convert your input into html entities, and then when it is outputted it will be interpreted as HTML and outputted as the result of that HTML...For example:
$mything = "<b>BOLD & BOLD</b>";
//normally would throw an error if not converted...
//lets convert!!
$mynewthing = htmlentities($mything);
Now, just insert $mynewthing to your database!!
htmlentities is basically as superset of htmlspecialchars, and htmlspecialchars replaces also < and >.
Actually, what you are trying to do is to fix invalid HTML code, and I think this needs an ad-hoc solution:
$row['details'] = preg_replace("/&(?![#0-9a-z]+;)/i", "&", $row['details']);
This is not a perfect solution, since it will fail for strings like: someone&son; (with a trailing ;), but at least it won't break existing HTML entities.
However, if you have decision power over how the data is stored, please enforce that the HTML code stored in the database is correct.
In my Projects I use XSLT Parser, so i had to change to (e.g.). But this is the safety way i found...
here is my code
$html = trim(addslashes(htmlspecialchars(
html_entity_decode($_POST['html'], ENT_QUOTES, 'UTF-8'),
ENT_QUOTES, 'UTF-8'
)));
And when you read from DB, don't forget to use stripslashes();
$html = stripslashes($mysq_row['html']);
I have saved some information in database with MySQL, now i want to show them, cause it contains some tags like <div>, <p>, etc. I just want them showed as raw html code, anyone can tell me how? i try to use `html_entity_decode(), but it does not work.
Example:
<div><b>Prénom/Nom : </b>tantantan tan</div>
<div><b>Pseudonyme : </b>nickname</div>
<div><b>Résidence principale : </b>69001 Lyon 1er</div>
<div><b>Autre résidence : </b> Place bellecours 69002 Lyon 2e</div>
====== in fact , i need to do in this way.
#using serialize() method
$data = serialize($_SESSION);
$sql = "Insert into sessioninfo `data` values('$data')";
and then
# I assume you can retrieve the data from database and assign to the following variable
$data = unserialize($row['data']);
perfectly resolve my problem. thanks everyone.
This is not an mysql_real_escape_string data but html_specialchars() encoded data
you can do the reverse with htmlspecialchars_decode()
Try html_entity_decode:
echo html_entity_decode($string);
Would recommend not to apply htmlspecialchars when you save the database.
The sanitize should be applied when sending output, if necessary.