Regex: How to separate Capital letter words without Spaces in PHP

296

The following code snippet shows you how to use regular expression to split a string with no spaces, only capital letters. The result is stored into an array that preg_match_all created. Note that $matches is a multi-dimensional array created by preg_match_all.

<pre>
<?
$exp = "/[A-Z]([^[A-Z]+]*)?/";
$sub = "ThisIsATestString";
preg_match_all($exp,$sub,$matches);
print_r($matches[0]);
?>
</pre>

If you want to split the string, you could write the regular expression with a php preg_split

<pre>
<?
$exp = "/(?=[A-Z])/";
$sub = "ThisIsATest";
$array = preg_split($exp,$sub);
print_r($array);
?>
</pre>
Article written by developer and designer


0 comments ↓

If you found this post useful click the share this button. Contribute below by adding a comment, no registration is required.

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment