Jump to content

Recommended Posts

Hi, I need to match stuff between curly brackets.

 

So basically I'm trying this:

 

$string = "{PAGE_TITLE} - {PAGE_NAME}";
preg_match( '/{PAGE_(.*)}/', $string, $array );

print_r($array);

 

Which leaves me with this:

Array
(
    [0] => {PAGE_TITLE} - {PAGE_NAME}
    [1] => TITLE} - {PAGE_NAME
)

 

Which is obviously not the result I want.

 

What I want is this:

Array
(
    [0] => {PAGE_TITLE}
    [1] => {PAGE_NAME}
)

 

Any help is appreciated,

 

Chris

Link to comment
https://forums.phpfreaks.com/topic/214185-preg-help/
Share on other sites

I think we should use preg_match_all to match more instances of a subpattern:

$string = "lalala {PAGE_TITLE} - {PAGE_NAME} lalalsa";
        preg_match_all( '/({PAGE_.*?})/', $string , $array);

        print_r($array);

 

Note though that [0] prints out the shows all the substrings that matched the pattern.

 

Link to comment
https://forums.phpfreaks.com/topic/214185-preg-help/#findComment-1114571
Share on other sites

OK i have another solution that meet your needs more, but it's maybe not the best way of doing it.

 

$stringy = "lalala {PAGE_TITLE} - {PAGE_NAME} lalalsa";
            $array_stringy = explode(" ", $stringy); // put string in an array delimiter is the 'space'
            //print_r ($array_stringy);
            $fl_array = preg_grep("/({PAGE_.*?})/", $array_stringy); // select only the pieces from the array that match your pattern
            $clean_array = array_values($fl_array); // resseting keys of array
            print_r ($fl_array); // unsorted looking array
            echo '<br>'; //break for some order
            print_r($clean_array); //clean aray with new keys starting at 0

 

If anyone knows a better way please let me know. I have a feeling this is inefficient  ::)

Link to comment
https://forums.phpfreaks.com/topic/214185-preg-help/#findComment-1114580
Share on other sites

oh that's very smart! ::)

 

Just for my knowledge, if i split it up in parts is this the correct interpretation?

1)  '~{  }~'  // enclose the pattern in { and }

2)  [^}]+  // does this mean it may not contain }  , and that the inside must at least appear 1 or more time ?

Link to comment
https://forums.phpfreaks.com/topic/214185-preg-help/#findComment-1114630
Share on other sites

~{[^}]+}~


~     starting delimiter
{     match a literal open curly brace
[^}]  negative character class meaning match anything that is not a closing curly brace
+     match one or more of the previous thing, so match one or more of anything that is not a closing curly brace.
}     match a literal closing curly brace
~     ending delimiter 

Link to comment
https://forums.phpfreaks.com/topic/214185-preg-help/#findComment-1114634
Share on other sites

$string = "{PAGE_TITLE} - {PAGE_NAME}";
preg_match_all( '~{[^}]+}~', $string, $array);
print_r($array);

 

Array
(
    [0] => Array
        (
            [0] => {PAGE_TITLE}
            [1] => {PAGE_NAME}
        )

)

Thanks! And everyone else!

Link to comment
https://forums.phpfreaks.com/topic/214185-preg-help/#findComment-1114920
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.