How do I remove  from the beginning of a file?

前端 未结 23 1033
攒了一身酷
攒了一身酷 2020-11-22 06:21

I have a CSS file that looks fine when I open it using gedit, but when it\'s read by PHP (to merge all the CSS files into one), this CSS has the following characters prepend

相关标签:
23条回答
  • 2020-11-22 06:46

    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.

    0 讨论(0)
  • 2020-11-22 06:47

    In PHP, you can do the following to remove all non characters including the character in question.

    $response = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $response);
    
    0 讨论(0)
  • 2020-11-22 06:49

    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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    
    # fixed
    $xml_string = "<" . "?xml version=\"1.0\" encoding=\"UTF-8\"?" . ">";
    
    0 讨论(0)
  • 2020-11-22 06:53

    For me, this worked:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    

    If I remove this meta, the  appears again. Hope this helps someone...

    0 讨论(0)
  • 2020-11-22 06:57

    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.

    0 讨论(0)
  • 2020-11-22 06:59

    Open your file in Notepad++. From the Encoding menu, select Convert to UTF-8 without BOM, save the file, replace the old file with this new file. And it will work, damn sure.

    0 讨论(0)
提交回复
热议问题