4

I've been struggling with this for like half a day already and I can't seem to find the answer. Please do help a noob. :)

I've got a string which consists of few sentences which are in curly brackets. It looks something like this:

{Super duper extra text.} {Awesome another text!} {And here we go again...}

Now I want to split it.

I figured out I could search for patterns like .} {, etc. So I did it like this:

$key = preg_split('/[!?.]{1,3}\} \{/',$key);

But this way I lost the delimiter, I lost all those . ! ? etc. at the end of the sentence.

I tried to do it like that:

$key = preg_split('/([!?.]{1,3}\} \{)/',$key, -1, PREG_SPLIT_DELIM_CAPTURE);
$sentences = array();

for ($i=0, $n=count($key)-1; $i<$n; $i+=2) {
$sentences[] = $key[$i].$key[$i+1]."<br><br>";
}

But this code never loads, so I gather something's wrong with it. But what?

Thanks in advance.

moskalak
  • 271
  • 2
  • 12

3 Answers3

6

You don't need to split this, just call preg_match() on it instead. The matched groups will result in an array. The expression grouped inside (), [^}]+ matches all characters up to, but not including, the next }. The output values you want will be inside the $matches[1] subarray.

$input = "{Super duper extra text.} {Awesome another text!} {And here we go again...}";
$matches = array();
preg_match_all('/\{([^}]+)\}/', $input, $matches);
print_r($matches);

Array
(
    [0] => Array
        (
            [0] => {Super duper extra text.}
            [1] => {Awesome another text!}
            [2] => {And here we go again...}
        )

    [1] => Array
        (
            [0] => Super duper extra text.
            [1] => Awesome another text!
            [2] => And here we go again...
        )

)
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • This works very well in my situation where I have a string that contains several JSON substrings concatenated together. For example: `'{"id":"Q3","name":"Question 3"} {"id":"Q4","name":"Question 4"}'`. The only caveat is that if there is a closing curly bracket within one of the substrings, the regex will stop on it. – Mike Finch Mar 28 '17 at 17:53
  • @MikeFinch That complicates things. If yours are concatenated but always adjacent `{}{}`, you could `preg_split('/\}\{/', $str)` to split up the string at the `}{` junction, but you would then need to restore the closing `}` or opening `{` which `preg_split()` would remove. If at all possible, try to make your data source output a proper JSON array of objects to begin with `[{...},{...},{...}]` – Michael Berkowski Mar 28 '17 at 18:04
  • It is indeed more complicated than it needs to be. One additional detail I did not mention before is that the data source adds a `'; | '` delimiter between the valid JSON substrings. Alas, I am stuck with the data source doing what it does. I had arrived at the same idea you suggested of using `preg_split()`, with a pattern of `"/\};[^{]*\{/"`. Then, I have to re-add the missing curly brackets. – Mike Finch Mar 31 '17 at 22:33
  • This turned out to be exactly what I needed. The first array was used to replace template tokens with data. The second array was used in a switch/case block to determine which tokens were being 'requested' for replacement by the template. – jbobbins Mar 13 '23 at 17:11
2

I doubt you need preg_split() for this. From what you describe, it sounds like:

 $array = explode('} {', trim($string, '{}'));

...will do the job.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
0

Try this is working, use before middle curly brackets forward slashes.

$keywords = preg_split("/[\{\}]/", "{hypertext} language, programming");
print_r($keywords);
wnull
  • 217
  • 6
  • 21
Indrajeet Singh
  • 2,958
  • 25
  • 25