ILMV Posted August 25, 2009 Share Posted August 25, 2009 Hello all, I need to find this: <somerandomtext:somemoretext and replace with this: <somerandomtext_somemoretext Where the : changes to a _, I don't want to search the entire document, just where a < exists, with any number of alphanumeric characters exists and then a :, nothing else. I am a super n00b with regex, need any more info I can provide it. Many thanks, Ben Quote Link to comment https://forums.phpfreaks.com/topic/171763-find-xml-namespace-and-replace/ Share on other sites More sharing options...
ILMV Posted August 25, 2009 Author Share Posted August 25, 2009 I have found this, which removes the : all together, which I guess solves the problem, but if anyone can help me modify it so it replaces it with an underscore that would be fantastic. $namespaceFree = preg_replace('/([<<\/])([a-z0-9]+):/i','$1$2',$xml); Quote Link to comment https://forums.phpfreaks.com/topic/171763-find-xml-namespace-and-replace/#findComment-905704 Share on other sites More sharing options...
Adam Posted August 25, 2009 Share Posted August 25, 2009 Using what you have, just add the underscore into the replacement param: $namespaceFree = preg_replace('/([<<\/])([a-z0-9]+):/i','$1_$2',$xml); Edit: No my mistake, didn't think about what the regexp was doing. 2 minutes! Edit 2: This should do the trick: $namespaceFree = preg_replace('/<([a-z0-9]+)[a-z0-9]+)/i', '<$1_$2', $xml); Quote Link to comment https://forums.phpfreaks.com/topic/171763-find-xml-namespace-and-replace/#findComment-905716 Share on other sites More sharing options...
ILMV Posted August 25, 2009 Author Share Posted August 25, 2009 Thanks MrAdam! I have tweaked it a bit to catch </ as well: $xml = preg_replace('/<([\w]+)[\w]+)/', '<$1_$2', $xml); $xml = preg_replace('/<\/([\w]+)[\w]+)/', '</$1_$2', $xml); Thanks again p.s. learnt a bit about regex too Quote Link to comment https://forums.phpfreaks.com/topic/171763-find-xml-namespace-and-replace/#findComment-905777 Share on other sites More sharing options...
Adam Posted August 25, 2009 Share Posted August 25, 2009 Heh no problem. I actually changed it from the \w character class after I realized that would match underscores as well (remembering that you'd said alphanumerical). To build on yours you could quite easily turn that into one single replace: $xml = preg_replace('/<(\/)?([a-z0-9]+)[a-z0-9]+)/i', '<$1$2_$3', $xml); Quote Link to comment https://forums.phpfreaks.com/topic/171763-find-xml-namespace-and-replace/#findComment-905781 Share on other sites More sharing options...
nrg_alpha Posted August 25, 2009 Share Posted August 25, 2009 To build on yours you could quite easily turn that into one single replace: $xml = preg_replace('/<(\/)?([a-z0-9]+)[a-z0-9]+)/i', '<$1$2_$3', $xml); This could also be simplified to: $xml = preg_replace('#</?[a-z0-9]+\K:#i', '_', $xml); This assumes you don't need the match the whole tag, but only the front end portion of it, as in </randomText: which would become </randomText_ This way, we get to axe the need for captures all together. If however we need to match both sides of the colon, the pattern could become: $xml = preg_replace('#</?[a-z0-9]+\K:(?=[a-z0-9]+)#i', '_', $xml); Quote Link to comment https://forums.phpfreaks.com/topic/171763-find-xml-namespace-and-replace/#findComment-905909 Share on other sites More sharing options...
Adam Posted August 25, 2009 Share Posted August 25, 2009 Heh out shone me as ever! Quote Link to comment https://forums.phpfreaks.com/topic/171763-find-xml-namespace-and-replace/#findComment-905924 Share on other sites More sharing options...
nrg_alpha Posted August 25, 2009 Share Posted August 25, 2009 lol It wasn't so much to out shine, rather to show something different. Quote Link to comment https://forums.phpfreaks.com/topic/171763-find-xml-namespace-and-replace/#findComment-905927 Share on other sites More sharing options...
Adam Posted August 26, 2009 Share Posted August 26, 2009 Ha no it's all good, led me on to learn the use of the \k character class. Quote Link to comment https://forums.phpfreaks.com/topic/171763-find-xml-namespace-and-replace/#findComment-906562 Share on other sites More sharing options...
nrg_alpha Posted August 26, 2009 Share Posted August 26, 2009 Yeah, I just only learned of that one myself not too long ago.. Comes in nice and handy more often than I realise (it's my new regex best friend... that is till I find something cooler and more useful down the line ). I wrote a blog post about it here in case it can help you out further. Quote Link to comment https://forums.phpfreaks.com/topic/171763-find-xml-namespace-and-replace/#findComment-906763 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.