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
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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.