How do I remove  from the beginning of a file?

前端 未结 23 1034
攒了一身酷
攒了一身酷 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:43

    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.

    0 讨论(0)
  • 2020-11-22 06:43
    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.
    0 讨论(0)
  • 2020-11-22 06:45

    This works for me!

    def removeBOMs(fileName):
         BOMs = ['',#Bytes as CP1252 characters
        'þÿ',
        'ÿþ',
        '^@^@þÿ',
        'ÿþ^@^@',
        '+/v',
        '÷dL',
        'Ýsfs',
        'Ýsfs',
        '^Nþÿ',
        'ûî(',
        '„1•3']
         inputFile = open(fileName, 'r')
         contents = inputFile.read()
         for BOM in BOMs:
             if not BOM in contents:#no BOM in the file...
                 pass
             else:
                 newContents = contents.replace(BOM,'', 1)
                 newFile = open(fileName, 'w')
                 newFile.write(newContents)
                 return None
    
    0 讨论(0)
  • 2020-11-22 06:46

    BOM is just a sequence of characters ($EF $BB $BF for UTF-8), so just remove them using scripts or configure the editor so it's not added.

    From Removing BOM from UTF-8:

    #!/usr/bin/perl
    @file=<>;
    $file[0] =~ s/^\xEF\xBB\xBF//;
    print(@file);
    

    I am sure it translates to PHP easily.

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

    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?.

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

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

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