PDA

View Full Version : دستکاری داکیومنت html با DomDocument و مشکل utf-8



Muhammad-Ali
یک شنبه 21 اردیبهشت 1393, 15:34 عصر
سلام
یک فایل html دارم که باید src های تگ img اون رو تغییر بدم. مشکلی که با DomDocument دارم اینکه حروف فارسی رو در خروجی درهم میکنه.


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>&Ucirc;Œ&Oslash;&acute;&Oslash;&sup3;&Ucirc;Œ&Oslash;&acute;&Oslash;&sup3;&Ucirc;Œ&Oslash;&sup3;&Oslash;&acute; <img alt="الله, خدا,پروردگار" class="decoded" src="/job/images/pootica_536e0c7b62294.jpg" title="عکس الله است"></p></body></html>

کسی بااین مسئله برخورد داشته؟

plague
یک شنبه 21 اردیبهشت 1393, 18:01 عصر
encoding صفحت رو utf8 بزار
خط اول (حتما اولین خط باشه ) بنویس


header('Content-type: text/html; charset=utf-8');

البته هیچ کدی نزاشتی من فقط میتونم حدس بزنم چیکار کردی و راهنماییت کنم ولی در کل همینه !

Muhammad-Ali
یک شنبه 21 اردیبهشت 1393, 21:23 عصر
مشکلی از ckeditor بودش که تگ متا واسه content تولید نمیکنه. دنبالش نرفتم ببینم چطوریه. فعلا با کد زیر مشکل رو حل کردم.


$HTML_Header = '
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head><body>';
$HTML_Body = $this->content;
$HTML_Footer = "</body></html>";

$page = $HTML_Header.$HTML_Body.$HTML_Footer;

$doc = new DOMDocument('1.0','utf-8');
$doc->formatOutput = true;
$doc->loadHTML($page);

$tags = $doc->getElementsByTagName('img');
foreach ($tags as $tag) {
//echo $tag->getAttribute('src').'<br>';
if(!preg_match("@^(/job/images/+)@i",$tag->getAttribute('src')))
{
//echo 'matched'.'<br>';
$ch = curl_init($tag->getAttribute('src'));
$fileName = 'website_'.uniqid().substr($tag->getAttribute('src'),strripos($tag->getAttribute('src'),'.'));
$fp = fopen(Yii::app()->basePath.'/../images/'.$fileName, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$tag->setAttribute('src',Yii::app()->baseUrl.'/images/'.$fileName);
$tag->setAttribute('alt',$this->imageAlt);
$tag->setAttribute('title',$this->imageTitle);
}
}

$this->content = $doc->saveHTML();