Jump to content

preg help


Chris92

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

Archived

This topic is now archived and is closed to further replies.

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