0

I have some changelog text in a string and I would like to split each changelog entry into an array.

Here's an example of the changelog text in a variable - $changelog_txt:

version 4.4.6 ( updated 05-08-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.

version 4.4.5 ( updated 05-01-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.

So, the resulting output would be an array that looks like this:

array(
    [0] => version 4.4.6 ( updated 05-08-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
    [1] => version 4.4.5 ( updated 05-01-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
)

Can anyone help or offer any suggestions?

4
  • preg_split(); php.net/manual/en/function.preg-split.php Commented May 14, 2020 at 16:40
  • @AdamWinter yes, I've seen that already but I'm struggling with the actual regular expression required for this... I was hoping for some guidance on the actual regular expression needed. Commented May 14, 2020 at 16:45
  • @Mat, I've added answer for you with preg_split() Commented May 14, 2020 at 16:46
  • What is the logic you want to split on? An empty line or should the structure of the listed be taken into account with the version and the hyphens? Can you share your code? Commented May 14, 2020 at 17:29

3 Answers 3

1

You can do that using preg_split() to make an array.

<?php
$str = <<<EOD
version 4.4.6 ( updated 05-08-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.

version 4.4.5 ( updated 05-01-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
EOD;
$keywords = array_map('trim',preg_split("/^\s*$/m", $str));
print_r($keywords);
?>

WORKING DEMO: https://3v4l.org/1mpZH

Regex Explanation:

/^\s*$/m

^ asserts position at start of a line

\s* matches any whitespace character (equal to [\r\n\t\f\v ])

* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)

$ asserts position at the end of a line Global pattern flags

m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this works on my sample data perfectly. However, would you mind explaining the regular expression and what it actually does, both for me and others reading this?
@Mat any specific reason for un-accepting my answer? is there any issue with the solution?
This only works if you have a blank line in between each element. What would you do if you didn't?
0

You could preg_replace_callback() to insert your own delimiter, then explode().

<?php
$oldString ='version 4.4.6 ( updated 05-08-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.

version 4.4.5 ( updated 05-01-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.';

$newString = preg_replace_callback('(version [0-9].[0-9].[0-9][0-9]?)', function($matches){return 'myUnlikelyDelimiter'.$matches[0];}, $oldString);

$array = explode('myUnlikelyDelimiter', $newString);

var_dump($array);
?>

2 Comments

I hadn't thought about doing it this way to be honest and I like the thinking behind it!
[0-9].[0-9].[0-9][0-9]? matches singledigit.singledigit.single/doubledigit
0
<?php
$input_lines = <<<EOD
version 4.4.6 ( updated 05-08-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD         actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.

version 4.4.5 ( updated 05-01-2020 )
- Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD  actions in Layers panel.
- Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
- Improved the Visual Builder scroll performance.
- Added vmin and vmax to css allowed units in module settings.
EOD;
preg_match_all('/(^[\s\S]*(?:\r*\n{2}))/U', $input_lines, $output_array);
print_r($output_array);

Result:

Array
(
    [0] => Array
        (
            [0] => version 4.4.6 ( updated 05-08-2020 )
                - Improved logic to keep collapse/expand state consistent for  Add/Clone/Delete/DnD actions in Layers panel.
                - Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
                - Improved the Visual Builder scroll performance.
                - Added vmin and vmax to css allowed units in module settings.


        )

    [1] => Array
        (
            [0] => version 4.4.6 ( updated 05-08-2020 )
                - Improved logic to keep collapse/expand state consistent for Add/Clone/Delete/DnD actions in Layers panel.
                - Updated Layers panel to allow selecting Goal and Winner for Split Testing from Layers panel.
                - Improved the Visual Builder scroll performance.
                - Added vmin and vmax to css allowed units in module settings.


        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.