Bazinga777
Bazinga777

Reputation: 5281

Setting Utf8 not working for arabic text in php

I am trying to scrape a webpage in arabic and everything works fine except the fact that when i echo the text what i get is a garbled up text even though i have set the header to UTF-8

Here is my code

<?php

    header ('Content-Type: text/html; charset=UTF-8'); 

    require 'vendor/autoload.php';

    use Goutte\Client;


    $client = new Client();

    $crawler = $client->request('GET', 'http://www.lebanonfiles.com');

    $news_container = $crawler->filter('#mcs4_container .line');

    $news_container->each(function($node) {

        echo $node->text();

    })
?>

What i get is this piece of garbled text enter image description here

Upvotes: 1

Views: 6006

Answers (2)

Qirel
Qirel

Reputation: 26450

  • ALL attributes must be set to UTF-8, on all levels of your application/script.
  • Save the document as UTF-8 or as UTF-8 w/o BOM (If you're using Notepad++, it's Format -> Convert to UTF-8)
    • Note that even though they are both UTF-8, they can behave differently!
  • The header in both PHP and HTML should be set to UTF-8
    • HTML: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    • PHP: header('Content-Type: text/html; charset=utf-8');
  • You may need to specify your charset in your php.ini file, using default_charset = "utf-8", although this is standard in PHP 5.6
  • Everything that can be set to a specific charset, should be set to the same.

There might be different aspects of your code that needs to be set to a specific charset.

Upvotes: 1

Alessandro
Alessandro

Reputation: 898

You should try this... try to put this line at beginning of your php file: ini_set('default_charset', 'UTF-8'); this may solve your issue.

Have a nice day.

Upvotes: 1

Related Questions