king.oslo Posted January 6, 2010 Share Posted January 6, 2010 Hello, I am looking to match the text and symbols between two tags, like so (tags are underlined, text are bold): =?UTF-8?B? TEXT & symbols ?= Will I be able to match the text with something like this? ~(?<=\=\?.+\?).+(?=\?\=)~ Quote Link to comment https://forums.phpfreaks.com/topic/187392-match-text-between-two-tags/ Share on other sites More sharing options...
cags Posted January 6, 2010 Share Posted January 6, 2010 The exact format of the pattern will be dependent on what an input string would look like. You are likely to have problems with that code due to your greedy assertions. How fixed is the opening tag? I assume they don't always include UTF-8?B. Why is your closing tag attempting to match twice. Given the information provided, ignoring the actual pattern you have so far, the answer I would have given is... #=\?UTF-8\?B\?(.+)?\?=# Quote Link to comment https://forums.phpfreaks.com/topic/187392-match-text-between-two-tags/#findComment-989561 Share on other sites More sharing options...
salathe Posted January 6, 2010 Share Posted January 6, 2010 [ot] Why is your closing tag attempting to match twice. That'll be a positive lookahead, looking for ?=[/ot] Quote Link to comment https://forums.phpfreaks.com/topic/187392-match-text-between-two-tags/#findComment-989574 Share on other sites More sharing options...
king.oslo Posted January 6, 2010 Author Share Posted January 6, 2010 Hello, The tags will vary. They will be this in format: =?charset?encoding?encoded text?= for example "=? utf-8 ? B ? This is an example text ?= Charset will be any charset Encoding will be either capital B or Q, Example text will vary I want to strip everything, but not the example text. What do you think? Thanks, Marius Quote Link to comment https://forums.phpfreaks.com/topic/187392-match-text-between-two-tags/#findComment-989579 Share on other sites More sharing options...
rajivgonsalves Posted January 6, 2010 Share Posted January 6, 2010 taking your example into consideration this should work <?php $string = '=? utf-8 ? B ? This is an example text ?='; $string = preg_replace("#=\? (.*?) \? (B|Q)? \? (.*?) \?=#", '$3', $string); echo $string; ?> Quote Link to comment https://forums.phpfreaks.com/topic/187392-match-text-between-two-tags/#findComment-989584 Share on other sites More sharing options...
salathe Posted January 6, 2010 Share Posted January 6, 2010 Out of interest, is the entire subject string that you're matching against just this tags/content combination? I.e. does the subject string start with the charset tag and end with ?= and there's only one of it? Quote Link to comment https://forums.phpfreaks.com/topic/187392-match-text-between-two-tags/#findComment-989592 Share on other sites More sharing options...
cags Posted January 6, 2010 Share Posted January 6, 2010 [ot] Why is your closing tag attempting to match twice. That'll be a positive lookahead, looking for ?=[/ot] D'oh of course it is, for some reason I thought both sets of ?= were escaped. I also didn't realise the first option was a positive look behind assertion (which in PHP PCRE cannot be a variable length unless I'm mistaken), I was obviously knocked for six by the characters used in the 'tags'. Perhaps I should take up drinking coffee in the morning? Quote Link to comment https://forums.phpfreaks.com/topic/187392-match-text-between-two-tags/#findComment-989600 Share on other sites More sharing options...
king.oslo Posted January 6, 2010 Author Share Posted January 6, 2010 how can I match everything apart from a sub-pattern? For example, how do I strip everything everything apart from Q or B that are enclosed by ?'s ? This example strip everything, but I want to strip everything apart from B or Q <?php $string = 'LotsOfLetters?B?LotsOfLetters'; $regex = '~\w+\?(B|Q)\?\w+~'; $replace = ''; ?> Thanks, Marius Quote Link to comment https://forums.phpfreaks.com/topic/187392-match-text-between-two-tags/#findComment-989662 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.