Is it correct to use single quotes for HTML attributes?

前端 未结 15 2685
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 05:57

Recently I\'ve been seeing a lot of this:


    

        
15条回答
  •  爱一瞬间的悲伤
    2020-12-01 06:35

    I know this is an old thread, but still very much relevant.

    If you want control over quotes in your generated HTML, you can use the sprintf() function in PHP (and similar calls available in many other languages):

    $html = sprintf('%s', $url, $text);
    

    Using sprintf() allows the format string to be easily modifiable by retrieving it from a database or configuration file, or via translation mechanisms.

    It is very readable, and allows either double or single quotes in the generated HTML, with very little change and never any escaping:

    $html = sprintf("%s", $url, $text);
    

提交回复
热议问题