Chris92 Posted September 23, 2010 Share Posted September 23, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214185-preg-help/ Share on other sites More sharing options...
fortnox007 Posted September 23, 2010 Share Posted September 23, 2010 Just te make it more clear. You want to match everything between brackets, or do you only want to match stuff between brackets starting with PAGE_ Quote Link to comment https://forums.phpfreaks.com/topic/214185-preg-help/#findComment-1114512 Share on other sites More sharing options...
Chris92 Posted September 23, 2010 Author Share Posted September 23, 2010 I only want to match stuff between brackets starting with PAGE_. Quote Link to comment https://forums.phpfreaks.com/topic/214185-preg-help/#findComment-1114514 Share on other sites More sharing options...
fortnox007 Posted September 23, 2010 Share Posted September 23, 2010 Ok ill start puzzeling -edit: wel this should work, but since you didn't specify what other caracters are allowed i used .* if you want to use a limited range of characters let me know. /({PAGE_(.*)})/ Quote Link to comment https://forums.phpfreaks.com/topic/214185-preg-help/#findComment-1114542 Share on other sites More sharing options...
fortnox007 Posted September 23, 2010 Share Posted September 23, 2010 Hmm i changed it a bit, '/({PAGE_.*?})/' but its not getting the second match yet. Working on it Quote Link to comment https://forums.phpfreaks.com/topic/214185-preg-help/#findComment-1114562 Share on other sites More sharing options...
fortnox007 Posted September 23, 2010 Share Posted September 23, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214185-preg-help/#findComment-1114571 Share on other sites More sharing options...
fortnox007 Posted September 23, 2010 Share Posted September 23, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/214185-preg-help/#findComment-1114580 Share on other sites More sharing options...
.josh Posted September 23, 2010 Share Posted September 23, 2010 $string = "{PAGE_TITLE} - {PAGE_NAME}"; preg_match_all( '~{[^}]+}~', $string, $array); print_r($array); Array ( [0] => Array ( [0] => {PAGE_TITLE} [1] => {PAGE_NAME} ) ) Quote Link to comment https://forums.phpfreaks.com/topic/214185-preg-help/#findComment-1114625 Share on other sites More sharing options...
fortnox007 Posted September 23, 2010 Share Posted September 23, 2010 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/214185-preg-help/#findComment-1114630 Share on other sites More sharing options...
.josh Posted September 23, 2010 Share Posted September 23, 2010 ~{[^}]+}~ ~ 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 Quote Link to comment https://forums.phpfreaks.com/topic/214185-preg-help/#findComment-1114634 Share on other sites More sharing options...
fortnox007 Posted September 23, 2010 Share Posted September 23, 2010 Thanks Crayon, ;-) i am getting a little better everyday at this regexp. Quote Link to comment https://forums.phpfreaks.com/topic/214185-preg-help/#findComment-1114637 Share on other sites More sharing options...
Chris92 Posted September 24, 2010 Author Share Posted September 24, 2010 $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! Quote Link to comment https://forums.phpfreaks.com/topic/214185-preg-help/#findComment-1114920 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.