如何用PHP用大写字母分解字符串? [英] How does one break a string down by capital letters with PHP?

查看:117
本文介绍了如何用PHP用大写字母分解字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串:CamelCaseString

我想用大写字母explode(),split()或某种更好的方法将这个字符串分解成单个单词.

I want to explode(), split(), or some better method on capital letters to break this string down into the individual words.

最简单的方法是什么?

---解决方案更新---

此链接指向一个稍有不同的问题,但是我认为答案通常比此页面上当前问题的答案更有用: 如何我在大写字母的字符串中添加了一个空格,但是使用PHP和Regex将连续的大写字母保持在一起吗?

This link is to a slightly different question, but I think the answer will generally be more useful than the answers to the current question on this page: How can I add a space in string at capital letters, but keep continuous capitals together using PHP and a Regex?

推荐答案

您应使用正则表达式. 试试这个:preg_match_all('/[A-Z][^A-Z]*/', "CamelCaseString", $results);

You should use regular expressions. Try this: preg_match_all('/[A-Z][^A-Z]*/', "CamelCaseString", $results);

包含所有单词的数组将保存在$ results [0]中.

The array containing all the words will be saved in $results[0].

这篇关于如何用PHP用大写字母分解字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆