Jump to content

[SOLVED] Help with string splitting


eludlow

Recommended Posts

I have a string that takes the following form: {1}{2}{3}

 

I need to split the string into an array based on the curly brackets - I think this is best achieved using preg_split() - would that be correct?  This would also be one of my first forays into regular expressions - would someone be able to show me the regex needed and explain how it's achieving the split, please?

 

Many thanks in advance,

Ed Ludlow

Link to comment
https://forums.phpfreaks.com/topic/124641-solved-help-with-string-splitting/
Share on other sites

<pre>
<?php
$str = '{1}{2}{3}';
$pieces = preg_split('/[{}]/', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($pieces);
?>
</pre>

 

preg_* functions use delimiters, thus the /.../. [...] is a character class which matches one character out of its pool, in this case { or }.

 

You can find more details in my signature links.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.