PHP:将字符串设为大写,但其中的 html 实体不大写?
如何将字符串中的内容设置为大写,而不是其中的 html 实体?是否可以?
$str = 'FUNDA MENTALISM';
echo strtoupper($str);
我想生成这个,
'FUNDA MENTALISM'
但是我用 strtoupper()
得到这个
'FUNDA MENTALISM'
How can I make the content in a string to upper case but not the html entities in it? Is it possible?
$str = 'FUNDA MENTALISM';
echo strtoupper($str);
I want to produce this,
'FUNDA MENTALISM'
but I get this with strtoupper()
'FUNDA MENTALISM'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我知道您没有在标签中列出 CSS,但大多数时候将其留给客户端会更容易(如果您只想将此字符串用于浏览器显示)。
应用 CSS
text-transform: uppercase;
将为您完成此操作。I know you haven't listed CSS in your tags, but most of the time it is easier to leave this to the client side (if you only intended this string for browser display).
Applying CSS
text-transform: uppercase;
will do this for you.好吧,删除实体并使用多字节字符集!
然后输出字符串。大多数 html 实体不需要,只需使用本机字符并正确设置文档的输出即可。
如果您确实必须使用这些实体,则需要使用正则表达式:
请注意,我尚未测试该正则表达式,但它应该可以工作,因为它正在查找后面不紧跟着
;
字符的字母。 ..Well, remove the entities and use a multi-byte character set!
Then output the string. There's no need for most html entities, just use the native characters and set the output of the document properly.
If you really must use the entities, a regex is in order:
Note that I haven't tested that regex, but it should work since it's looking for letters that are not immediately followed by a
;
character...我无法使用 kapa 的 CSS 变体,因为我需要它作为标题标签。 ircmaxell 提供的解决方案可能是正确的,但由于某些服务器没有
mbstring
扩展,因此此解决方案可能更好:I cannot use the CSS variant by kapa, 'cause I need this for the title tag. The solution provided by ircmaxell might be right, but since some servers don't have
mbstring
extension, this solution might be better:最好先将字符串转换为大写而不是解码,这样您就可以获得所需的结果
function uppercase
strtoupper($var);
输出将是function htmlEntities()
$var=htmlEntities($var) ;
输出:最终编码
$var=html_entity_decode($var);
输出:
THISISTEST
NEW LINE
如果第一个 htmlentities 转换为大写;解码将失败,因为编码测试更改为大写且功能失败;
It is better to convert string to uppercase first than decode than you will get desired result
function uppercase
strtoupper($var);
Output will befunction htmlEntities()
$var=htmlEntities($var);
OUTPUT:FINAL Encoding
$var=html_entity_decode($var);
output:
THISISTEST
NEW LINE
If first htmlentities convert than upper case; decode will fail as encoded test changed to uppercase and function failed;
我创建了这个函数来替换“strtoupper”,对于重音字母没有任何问题。它比 html_entity_decode > 效果更好上层> htmlentities ,它不处理某些字符 (Ò) :
I created this function to replace "strtoupper" without any problem for accented letters. It works better than html_entity_decode > strtoupper > htmlentities , which did not handle certain characters (Ò) :