简体   繁体   English

正则表达式和大小写

[英]regex and upper and lower case

i'm using this 我正在使用这个

preg_match('/<meta\s+http-equiv="Content-Type" content="([^;]+)(?:;\s*charset=["]?([^"^\s]*))?"/i', $html, $match)

to get the charset but this will not work with this 得到字符集,但这不适用于此

<META http-equiv=Content-Type content="text/html; charset=windows-1256">

any ideas? 有任何想法吗?

You have double quotes in preg_match() but not in html example. 您在preg_match()中有双引号但在html示例中没有。

Here: http-equiv=Content-Type 这里: http-equiv = Content-Type

If You want to support preg_match() to work with and without quotes here, simple change code to this: 如果你想支持preg_match()在这里使用和不使用引号,可以简单地将代码更改为:

preg_match('/<meta\s+http-equiv=(")?Content-Type(")?\s+content="([^;]+)(?:;\s*charset=["]?([^"^\s]*))?"/i', $html, $match);

Better version that will work when attributes order change: 在属性订单更改时将使用的更好的版本:

preg_match('/<meta.+?content="([^;]+)(?:;\s*charset=["]?([^"^\s]*))?"/i', $html, $match);

If you are only interested in getting the charset, why not using something simpler like: 如果你只对获取字符集感兴趣,为什么不使用更简单的东西:

preg_match('/charset=([^"]+)/i', $html, $match);

Of course, you can tweak it to meet your specific needs, but keep it simple and it'll be much easier to get it to work. 当然,您可以调整它以满足您的特定需求,但要保持简单,让它更容易上班。

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

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