REGEX Help - How Do I Do this?

I want to find the following using a regex, how do I do this?
here is some text { best | worst | most awesome} here is some text

Open in new window


I am looking to take the words out of the {} and push them into an array in php. So my end result will be :
array("best", "worst", "most awesome")

Open in new window


I know how to make an array in php, I'm just not sure how to code up the regex part. I want to specifically look for the pipes in the string between the two curly braces, so something like {my words} would not be found but {my | words} would be.
EffinGoodAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Please see: http://iconoun.com/demo/temp_effingood.php
<?php // demo/temp_effingood.php

/**
 * http://www.experts-exchange.com/questions/28712538/REGEX-Help-How-Do-I-Do-this.html
 */
error_reporting(E_ALL);
echo '<pre>';


// TEST DATA, AUGMENTED FROM THE POST AT E-E
$txt = <<<EOD
here is some text { best | worst | most awesome} here is some text
and here is some non-text { no pipes here } for the test
some text { with | two } { piped | substrings }
EOD;


// A REGULAR EXPRESSION
$rgx
= '#'               // REGEX DELIMITER
. '.*?'             // ANYTHING OR NOTHING
. preg_quote('{')   // OPENING BRACE
. '('               // START CAPTURE GROUP
. '.*?'             // ANYTHING OR NOTHING
. preg_quote('|')   // A PIPE CHARACTER
. '.*?'             // ANYTHING OR NOTHING
. ')'               // ENDOF CAPTURE GROUP
. preg_quote('}')   // CLOSING BRACE
. '#'               // REGEX DELIMITER\
;


// PROCESS EACH LINE ONE AT A TIME
$arr = explode(PHP_EOL, $txt);
foreach ($arr as $str)
{
    // COLLECT RESULTS HERE
    $out = [];

    // NORMALIZE THE STRINGS
    $str = trim($str);
    echo PHP_EOL;
    echo '<b>' . $str . '</b>';

    // CAPTURE THE MATCHES
    preg_match_all($rgx, $str, $mat);

    // OPTIONAL - SHOW THE INTERMEDIATE WORK PRODUCTS
    // var_dump($mat);

    // CREATE ARRAYS FROM THE CAPTURED MATCHES
    foreach ($mat[1] as $cap)
    {
        $cap = explode('|', $cap);
        foreach ($cap as $sub)
        {
            $out[] = trim($sub);
        }
    }
    echo PHP_EOL;
    print_r($out);
    echo PHP_EOL;
}

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EffinGoodAuthor Commented:
If I could give you an A+++++, I would! Thanks Ray, you rock.
0
Ray PaseurCommented:
Glad to help, but as is often the case, the devil is in the details with something like this.  Probably wise to set up as many comprehensive test cases as you can!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

Best of luck with your project!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.