简体   繁体   English

无效的HTML-引用属性

[英]Invalid HTML - Quoting Attributes

I have following HTML: 我有以下HTML:

<td width=140 style='width:105.0pt;padding:0cm 0cm 0cm 0cm'>
    <p class=MsoNormal><span style='font-size:9.0pt;font-family:"Arial","sans-serif";
       mso-fareast-font-family:"Times New Roman";color:#666666'>OCCUPANCY
       TAX:</span></p>
</td>

Some of the HTML attributes are not quoted, like for example: width=140 and class=MsoNormal 未引用某些HTML属性,例如:width = 140和class = MsoNormal

Are there any PHP function for that sort of thing, if not what would be the clever way of sanitizing this in HTML? 如果没有的话,是否有任何PHP函数可以解决此类问题,如果不是,那么用HTML进行清理的聪明方法是什么?

Thank you. 谢谢。

I guess you could use regexp for this: 我猜你可以为此使用正则表达式:

/\s([\w]{1,}=)((?!")[\w]{1,}(?!"))/g


\s match any white space character [\r\n\t\f ]
1st Capturing group ([\w]{1,}=)
    [\w]{1,} match a single character present in the list below
        Quantifier: {1,} Between 1 and unlimited times, as many times as possible, giving back as needed [greedy]
    \w match any word character [a-zA-Z0-9_]
    = matches the character = literally
2nd Capturing group ((?!")[\w]{1,}(?!"))
    (?!") Negative Lookahead - Assert that it is impossible to match the regex below
    " matches the characters " literally
    [\w]{1,} match a single character present in the list below
        Quantifier: {1,} Between 1 and unlimited times, as many times as possible, giving back as needed [greedy]
    \w match any word character [a-zA-Z0-9_]
    (?!") Negative Lookahead - Assert that it is impossible to match the regex below
    " matches the characters " literally
g modifier: global. All matches (don't return on first match)

Which would be implemented something like this: 可以这样实现:

echo preg_replace_callback('/\s([\w]{1,}=)((?!")[\w]{1,}(?!"))/', function($matches){
    return ' '.$matches[1].'"'.$matches[2].'"';
}, $str);

And would result in: 并导致:

 <td width="140" style='width:105.0pt;padding:0cm 0cm 0cm 0cm'>
   <p class="MsoNormal"><span style='font-size:9.0pt;font-family:"Arial","sans-serif";
     mso-fareast-font-family:"Times New Roman";color:#666666'>OCCUPANCY
      TAX:</span></p>
 </td>

Eval.in live example 现场实例评估

Note, this is a down and dirty example, and can surely be cleaned up. 注意,这是一个肮脏的例子,可以肯定地将其清除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM