glenelkins Posted May 27, 2009 Share Posted May 27, 2009 hi say i have this string: {cms type="textarea" name="qdos_entertainment_plc_para_1" cols="75" rows="5" style="border: 1px solid #000000;padding: 5px;"} I want to match this: {cms HERE CAN HAVE ANYTHING IN ANY ORDER BUT MUST HAVE name="NAME_HERE"} ANYTHING HERE {/cms} what would be the expression? Quote Link to comment https://forums.phpfreaks.com/topic/159840-pattern-match/ Share on other sites More sharing options...
aschk Posted May 27, 2009 Share Posted May 27, 2009 Something like this: "/{cms(.+)[^(name=\"(.*)\")](.+)}/" Probably won't work but might give a basis for whatever you're after. Quote Link to comment https://forums.phpfreaks.com/topic/159840-pattern-match/#findComment-843013 Share on other sites More sharing options...
glenelkins Posted May 27, 2009 Author Share Posted May 27, 2009 basically i need it to match {cms name="bla bla"}(.*){/cms} the part with the name bit is essential but there could be more params before or after it which should just be ignored so for example, this is not valid {cms type="textarea" rows="10" cols="5"}CONTENT TO RETURN HERE{/cms} But this is valid {cms name="blb lfdnjk" type="textarea" rows="10" cols="5"}CONTENT TO RETURN HERE{/cms} And this is valid {cms type="textarea" rows="10" cols="5" name="bla bla"}CONTENT TO RETURN HERE{/cms} and this is also valid {cms name="bla bla"}CONTWENT{/cms} Quote Link to comment https://forums.phpfreaks.com/topic/159840-pattern-match/#findComment-843015 Share on other sites More sharing options...
Adam Posted May 27, 2009 Share Posted May 27, 2009 Give this a try... preg_match('/\{cms[^\}]*name="[^"]+"[^\}]*\}[^\{]+\{\/cms\}/', $str); Quote Link to comment https://forums.phpfreaks.com/topic/159840-pattern-match/#findComment-843017 Share on other sites More sharing options...
glenelkins Posted May 27, 2009 Author Share Posted May 27, 2009 MrAdam that is almost there i think. Check my longer post above, basically it needs to find the tag with the name="whatever" and return whats between the {cms}...{/cms} bit so say i have {cms name="test" type="bla bla"}CONTENT HERE{/cms} I have a var like $name = 'test' it needs to go through the content i have looking for the cms tag with the name="$name" and return whats between the beginning and end tags, but the name parameter is allowed to fall anywhere within the first tag before or after any other param Quote Link to comment https://forums.phpfreaks.com/topic/159840-pattern-match/#findComment-843019 Share on other sites More sharing options...
Adam Posted May 27, 2009 Share Posted May 27, 2009 The regular expressions is right, it's just modifying the function a little bit now to return the matches. preg_match_all('/\{cms[^\}]*name="[^"]+"[^\}]*\}[^\{]+\{\/cms\}/', $str, $matches); print_r($matches); The $matches array will contain what you're after... Quote Link to comment https://forums.phpfreaks.com/topic/159840-pattern-match/#findComment-843022 Share on other sites More sharing options...
glenelkins Posted May 27, 2009 Author Share Posted May 27, 2009 but it doesnt! if i have the tag as this: {cms type="textarea" name="qdos_entertainment_plc_para_1" cols="75" rows="5" style="border: 1px solid #000000;padding: 5px;"}Chaired by Nick Thomas, the company he created in 1986 is now one of the largest, broad-based entertainment Groups in the UK. Focussed on family entertainment and traditional values, the Group has grown both organically and through a steady 'buy & build' strategy focusing on businesses that complement the original model, managed by a coherent holding company.{/cms} the array only comes back with position 0 there are no other elements so all its doing is returning the entire string i pasted above. what i want to do is get the actual content between the tags and replace it with something else Quote Link to comment https://forums.phpfreaks.com/topic/159840-pattern-match/#findComment-843024 Share on other sites More sharing options...
aschk Posted May 27, 2009 Share Posted May 27, 2009 Put a bracket in the right place then preg_match_all('/\{cms[^\}]*name="([^"]+)"[^\}]*\}[^\{]+\{\/cms\}/', $str, $matches); Quote Link to comment https://forums.phpfreaks.com/topic/159840-pattern-match/#findComment-843026 Share on other sites More sharing options...
Adam Posted May 27, 2009 Share Posted May 27, 2009 Oh sorry I see. Try this: preg_match_all('/\{cms[^\}]*name="[^"]+"[^\}]*\}([^\{]+)\{\/cms\}/', $str, $matches); print_r($matches); Let me know if this is what you're after... Quote Link to comment https://forums.phpfreaks.com/topic/159840-pattern-match/#findComment-843027 Share on other sites More sharing options...
glenelkins Posted May 27, 2009 Author Share Posted May 27, 2009 ah i got it i think! /\{cms[^\}]*name="[^"]+"[^\}]*\}(.*){\/cms\}/ Quote Link to comment https://forums.phpfreaks.com/topic/159840-pattern-match/#findComment-843028 Share on other sites More sharing options...
glenelkins Posted May 27, 2009 Author Share Posted May 27, 2009 ok here is 1 more issue with this the expression /\{cms[^\}]*name="[^"]+"[^\}]*\}(.*){\/cms\}/ seems to work, but if the content has a special charcter say an apostraphie, it seems to stop matching at the symbol and cuts the text off so in this example text below it cuts off right after "'buy" Chaired by Nick Thomas, the company he created in 1986 is now one of the largest, broad-based entertainment Groups in the UK. Focussed on family entertainment and traditional values, the Group has grown both organically and through a steady 'buy & build' strategy focusing on businesses that complement the original model, managed by a coherent holding company. Quote Link to comment https://forums.phpfreaks.com/topic/159840-pattern-match/#findComment-843029 Share on other sites More sharing options...
Adam Posted May 27, 2009 Share Posted May 27, 2009 The last example I sent you works. Quote Link to comment https://forums.phpfreaks.com/topic/159840-pattern-match/#findComment-843032 Share on other sites More sharing options...
glenelkins Posted May 27, 2009 Author Share Posted May 27, 2009 it doesnt! it cuts the text off like this: Chaired by Nick Thomas, the company he created in 1986 is now one of the largest, broad-based entertainment Groups in the UK. Focussed on family entertainment and traditional values, the Group has grown both organically and through a steady \'buy unless thats the javascript doing that when i send the string to the ajax file Quote Link to comment https://forums.phpfreaks.com/topic/159840-pattern-match/#findComment-843033 Share on other sites More sharing options...
glenelkins Posted May 27, 2009 Author Share Posted May 27, 2009 it was the js doing it. i just added the escape function thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/159840-pattern-match/#findComment-843034 Share on other sites More sharing options...
Adam Posted May 27, 2009 Share Posted May 27, 2009 You need to escape your single quotes. How exactly are you returning the string which contains the cms tags? EDIT: Excellent! Quote Link to comment https://forums.phpfreaks.com/topic/159840-pattern-match/#findComment-843035 Share on other sites More sharing options...
glenelkins Posted May 27, 2009 Author Share Posted May 27, 2009 another quick question... now i have the content from the middle and i have it replacing with preg_replace how can i actually return everything thats between {cms params here} because i dont actually want to replace the {cms PARAMS}...{/cms} bits just whats between them Quote Link to comment https://forums.phpfreaks.com/topic/159840-pattern-match/#findComment-843037 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.