سؤال

لدي ملف CSS يبدو جيدًا عندما أفتحه باستخدام gedit, ، ولكن عندما تتم قراءته بواسطة PHP (لدمج جميع ملفات CSS في ملف واحد)، فإن ملف CSS هذا يحتوي على الأحرف التالية مسبوقة به:أنا"

تقوم PHP بإزالة كافة المسافات البيضاء، لذا فإن وجود علامة  عشوائية في منتصف الكود يفسد الأمر برمته.كما ذكرت، لا أستطيع رؤية هذه الأحرف عندما أفتح الملف في gedit، لذلك لا أستطيع إزالتها بسهولة.

لقد بحثت في Google عن المشكلة، ومن الواضح أن هناك خطأ ما في تشفير الملف، وهو أمر منطقي لأنني قمت بنقل الملفات إلى خوادم Linux/Windows مختلفة عبر بروتوكول نقل الملفات و rsync, ، مع مجموعة من برامج تحرير النصوص.لا أعرف حقًا الكثير عن ترميز الأحرف، لذا سأكون موضع تقدير للمساعدة.

إذا كان ذلك مفيدًا، فسيتم حفظ الملف بتنسيق UTF-8، ولن يسمح لي gedit بحفظه بتنسيق ISO-8859-15 (يحتوي المستند على حرف واحد أو أكثر لا يمكن ترميزه باستخدام ترميز الأحرف المحدد).حاولت حفظه بنهايات سطر Windows وLinux، لكن لم يساعد أي منهما.

هل كانت مفيدة؟

المحلول

ثلاث كلمات لك:

علامة ترتيب البايت (BOM)

هذا هو تمثيل UTF-8 BOM في ISO-8859-1.يجب عليك إخبار المحرر الخاص بك بعدم استخدام BOMs أو استخدام محرر مختلف لإزالتها.

لأتمتة عملية إزالة BOM، يمكنك استخدامها awk كما هو موضح في هذا السؤال.

مثل تقول إجابة أخرى, ، الأفضل هو أن تقوم PHP بتفسير BOM بشكل صحيح، لذلك يمكنك استخدامه mb_internal_encoding(), ، مثله:

 <?php
   //Storing the previous encoding in case you have some other piece 
   //of code sensitive to encoding and counting on the default value.      
   $previous_encoding = mb_internal_encoding();

   //Set the encoding to UTF-8, so when reading files it ignores the BOM       
   mb_internal_encoding('UTF-8');

   //Process the CSS files...

   //Finally, return to the previous encoding
   mb_internal_encoding($previous_encoding);

   //Rest of the code...
  ?>

نصائح أخرى

افتح الملف الخاص بك في Notepad ++ .من القائمة القائمة، حدد تحويل إلى UTF-8 بدون bom ، وحفظ الملف، واستبدال الملف القديم هذا الملف الجديد.وسيعمل، بالتأكيد اللعنة.

في PHP ، يمكنك القيام بما يلي لإزالة جميع الأحرف غير المرئية بما في ذلك الحرف المعني.

giveacodicetagpre.

بالنسبة لأولئك الذين لديهم Access SHELL هنا هو أمر بسيط للعثور على جميع الملفات مع مجموعة BOM في دليل Public_html - تأكد من تغييره إلى ما هو مسارك الصحيح على الخادم الخاص بك هو

رمز:

giveacodicetagpre.

، وإذا كنت مرتاحا مع VI محرر، فتح الملف في السادس:

giveacodicetagpre.

وأدخل الأمر لإزالة BOM:

giveacodicetagpre.

احفظ الملف:

giveacodicetagpre.

BOM هو مجرد سلسلة من الأحرف ($ EF $ BB $ BB for UTF-8)، لذلك فقط قم بإزالةها باستخدام البرامج النصية أو تكوين المحرر حتى لا تتم إضافتها.

from إزالة BOM من UTF-8 :

giveacodicetagpre.

أنا متأكد من أنها تترجم إلى PHP بسهولة.

بالنسبة لي، هذا عملت:

giveacodicetagpre.

إذا قمت بإزالة هذه التعريف، تظهر ï »¿مرة أخرى.آمل أن يساعد هذا شخص ما ...

أنا لا أعرف php، لذلك لا أعرف إذا كان هذا ممكنا، ولكن أفضل حل سيكون لقراءة الملف باسم UTF-8 بدلا من الترميز الآخر.BOM هو في الواقع عرض صفر لا توجد مساحة استراحة.هذه بيضاء، لذلك إذا تم قراءة الملف في الترميز الصحيح (UTF-8)، فسيتم تفسير BOM على أنها مسافة بيضاء وسيتم تجاهلها في ملف CSS الناتج.

أيضا، ميزة أخرى لقراءة الملف في الترميز الصحيح هو أنه لا داعي للقلق بشأن سوء تفسير الأحرف.يخبرك المحرر بك أن صفحة الرموز التي تريد حفظها لن تقوم بها جميع الأحرف التي تحتاج إليها.إذا تم قراءة PHP الملف في الترميز غير الصحيح، فمن المحتمل جدا أن يتم إساءة تفسير الأحرف الأخرى إلى جانب التفسير بصمت.استخدم UTF-8 في كل مكان، وهذه المشاكل تختفي.

يمكنك استخدام

giveacodicetagpre.

استبدال AWK يبدو أن العمل، لكنه ليس في مكانه.

grep -rl $'\xEF\xBB\xBF' * | xargs vim -e -c 'argdo set fileencoding=utf-8|set encoding=utf-8| set nobomb| wq'

I had the same problem with the BOM appearing in some of my PHP files ().

If you use PhpStorm you can set at hotkey to remove it in Settings -> IDE Settings -> Keymap -> Main Menu - > File -> Remove BOM.

In Notepad++, choose the "Encoding" menu, then "Encode in UTF-8 without BOM". Then save.

See Stack Overflow question How to make Notepad to save text in UTF-8 without BOM?.

Open the PHP file under question, in Notepad++.

Click on Encoding at the top and change from "Encoding in UTF-8 without BOM" to just "Encoding in UTF-8". Save and overwrite the file on your server.

Same problem, different solution.

One line in the PHP file was printing out XML headers (which use the same begin/end tags as PHP). Looks like the code within these tags set the encoding, and was executed within PHP which resulted in the strange characters. Either way here's the solution:

# Original
$xml_string = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;";

# fixed
$xml_string = "<" . "?xml version=\"1.0\" encoding=\"UTF-8\"?" . ">";

If you need to be able to remove the BOM from UTF-8 encoded files, you first need to get hold of an editor that is aware of them.

I personally use E Text Editor.

In the bottom right, there are options for character encoding, including the BOM tag. Load your file, deselect Byte Order Marker if it is selected, resave, and it should be done.

Alt text http://oth4.com/encoding.png

E is not free, but there is a free trial, and it is an excellent editor (limited TextMate compatibility).

You can open it by PhpStorm and right-click on your file and click on Remove BOM...

Here is another good solution for the problem with BOM. These are two VBScript (.vbs) scripts.

One for finding the BOM in a file and one for KILLING the damned BOM in the file. It works pretty fine and is easy to use.

Just create a .vbs file, and paste the following code in it.

You can use the VBScript script simply by dragging and dropping the suspicious file onto the .vbs file. It will tell you if there is a BOM or not.

' Heiko Jendreck - personal helpdesk & webdesign
' http://www.phw-jendreck.de
' 2010.05.10 Vers 1.0
'
' find_BOM.vbs
' ====================
' Kleines Hilfsmittel, welches das BOM finden soll
'
 Const UTF8_BOM = ""
 Const UTF16BE_BOM = "þÿ"
 Const UTF16LE_BOM = "ÿþ"
 Const ForReading = 1
 Const ForWriting = 2
 Dim fso
 Set fso = WScript.CreateObject("Scripting.FileSystemObject")
 Dim f
 f = WScript.Arguments.Item(0)
 Dim t
 t = fso.OpenTextFile(f, ForReading).ReadAll
 If Left(t, 3) = UTF8_BOM Then
     MsgBox "UTF-8-BOM detected!"
 ElseIf Left(t, 2) = UTF16BE_BOM Then
     MsgBox "UTF-16-BOM (Big Endian) detected!"
 ElseIf Left(t, 2) = UTF16LE_BOM Then
     MsgBox "UTF-16-BOM (Little Endian) detected!"
 Else
     MsgBox "No BOM detected!"
 End If

If it tells you there is BOM, go and create the second .vbs file with the following code and drag the suspicios file onto the .vbs file.

' Heiko Jendreck - personal helpdesk & webdesign
' http://www.phw-jendreck.de
' 2010.05.10 Vers 1.0
'
' kill_BOM.vbs
' ====================
' Kleines Hilfmittel, welches das gefundene BOM löschen soll
'
Const UTF8_BOM = ""
Const ForReading = 1
Const ForWriting = 2
Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Dim f
f = WScript.Arguments.Item(0)
Dim t
t = fso.OpenTextFile(f, ForReading).ReadAll
If Left(t, 3) = UTF8_BOM Then
    fso.OpenTextFile(f, ForWriting).Write (Mid(t, 4))
    MsgBox "BOM gelöscht!"
Else
    MsgBox "Kein UTF-8-BOM vorhanden!"
End If

The code is from Heiko Jendreck.

In PHPStorm, for multiple files and BOM not necessarily at the beginning of the file, you can search \x{FEFF} (Regular Expression) and replace with nothing.

Same problem, but it only affected one file so I just created a blank file, copy/pasted the code from the original file to the new file, and then replaced the original file. Not fancy but it worked.

Use Total Commander to search for all BOMed files:

Elegant way to search for UTF-8 files with BOM?

  • Open these files in some proper editor (that recognizes BOM) like Eclipse.

  • Change the file's encoding to ISO (right click, properties).

  • Cut  from the beginning of the file, save

  • Change the file's encoding back to UTF-8

...and do not even think about using n...d again!

I had the same problem. The problem was because one of my php files was in utf-8 (the most important, the configuaration file which is included in all php files).

In my case, I had 2 different solutions which worked for me :

First, I changed the Apache Configuration by using AddDefaultCharsetDirective in configuration files (or in .htaccess). This solution forces Apache to use the correct encodage.

AddDefaultCharset ISO-8859-1

The second solution was to change the bad encoding of the php file.

  1. Copy the text of your filename.css file.
  2. Close your css file.
  3. Rename it filename2.css to avoid a filename clash.
  4. In MS Notepad or Wordpad, create a new file.
  5. Paste the text into it.
  6. Save it as filename.css, selecting UTF-8 from the encoding options.
  7. Upload filename.css.

Check on your index.php, find "... charset=iso-8859-1" and replace it with "... charset=utf-8".

Maybe it'll work.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top