benmartin Posted July 10, 2008 Share Posted July 10, 2008 I do not like to resort to asking questions in a forum like this, but I am exhausted in trying to find how to do this on my own. I have zero familiarity with RegEx, but it seems that I need it to find a solution. I've never needed it until now. Basic overview: we have started using Adobe InDesign and have a number of publications that need to be migrated to the web. The XML (more specifically - xml cross-media) output is full of worthless tags / stylesheet information / classes for every tag, that vary widely depending on the styles used by production for the print product. Example: These are four different styles for different stories' headlines (the number denotes the font size): p.head-styles-head-24 {} p.head-styles-head-26 {} p.head-styles-head-28 {} p.head-styles-head-32 {} here are the headlines, along with the paragraph + class tags (not in the same order, but just to get an idea): <p class="head-styles-head-32">Pending sdssdasdfas Legislation</p> <p class="head-styles-head-32">SourcesasfasfsaasPackage</p> <p class="head-styles-head-24">Firm Blood asdasdasCompetition</p> etc. What I would like to do is strip everything after class="head-styles from the < p > tag and add my own identifier (for further parsing) essentially replacing <p class="head-styles*****All other characters before the quote****"> to something like <p %%%headline%%%> it would be the equivalent of saying (where wild = %) : find the string : <p class="head-styles%"> replace that with: <p **whatever I specify**> (I would believe that I'd be able to tell how to change what I want in there for other classes that exist, I just need a template). it must be able to check against the "head-styles" because there are other tags like "body-styles". Any help is MUCH appreciated. I've been working with this for days and have made zero progress. I really hope this makes sense. Thanks. -Ben Quote Link to comment https://forums.phpfreaks.com/topic/114111-solved-find-and-replace-within-a-string-maintaining-beginningend-of-said-string/ Share on other sites More sharing options...
effigy Posted July 10, 2008 Share Posted July 10, 2008 Something like this? <pre> <?php $data = <<<DATA <p class="head-styles-head-32">Pending sdssdasdfas Legislation</p> <p class="head-styles-head-32">SourcesasfasfsaasPackage</p> <p class="head-styles-head-24">Firm Blood asdasdasCompetition</p> DATA; $data = preg_replace('/(<p[^>]+class="head-styles)[^"]+/', '$1-new_style', $data); echo htmlspecialchars($data); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/114111-solved-find-and-replace-within-a-string-maintaining-beginningend-of-said-string/#findComment-586512 Share on other sites More sharing options...
benmartin Posted July 10, 2008 Author Share Posted July 10, 2008 Effigy, Thanks for the quick response. Almost but not quite. I've modified the code a little bit and I've added the output here as well. ----- code now (basically just changed the replace value) --- <pre> <?php $data = <<<DATA <p class="head-styles-head-32">Pending sdssdasdfas Legislation</p> <p class="head-styles-head-32">SourcesasfasfsaasPackage</p> <p class="head-styles-head-24">Firm Blood asdasdasCompetition</p> DATA; $data = preg_replace('/(<p[^>]+class="head-styles)[^"]+/', '<p myvalue>', $data); echo htmlspecialchars($data); ?> </pre> --------- output EDIT:::: Sorry, that is supposed to be : <p myvalue>">Pending sdssdasdfas Legislation</p> Not <p myvalue>">Pending sdssdasdfas Legislation</p> <p myvalue>">SourcesasfasfsaasPackage</p> <p myvalue>">Firm Blood asdasdasCompetition</p> Notice the (edit) "> in green. I would like to have those removed. It is removed from the front however. Aside from that, it's perfect. I appreciate your help very much. -Ben Quote Link to comment https://forums.phpfreaks.com/topic/114111-solved-find-and-replace-within-a-string-maintaining-beginningend-of-said-string/#findComment-586541 Share on other sites More sharing options...
effigy Posted July 10, 2008 Share Posted July 10, 2008 Are there no other attributes you need to retain? <pre> <?php $data = <<<DATA <p class="head-styles-head-32">Pending sdssdasdfas Legislation</p> <p class="head-styles-head-32">SourcesasfasfsaasPackage</p> <p class="head-styles-head-24">Firm Blood asdasdasCompetition</p> DATA; $data = preg_replace('/<p[^>]+class="head-styles[^>]+>/', '<p myvalue>', $data); echo htmlspecialchars($data); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/114111-solved-find-and-replace-within-a-string-maintaining-beginningend-of-said-string/#findComment-586552 Share on other sites More sharing options...
benmartin Posted July 10, 2008 Author Share Posted July 10, 2008 No, that's all I was looking for. What I will be looking for now is a good tutorial on this for the future. It seems like it can be pretty powerful. Effigy, thanks so much for this. I am very grateful that you could help me. Take care, -Ben Quote Link to comment https://forums.phpfreaks.com/topic/114111-solved-find-and-replace-within-a-string-maintaining-beginningend-of-said-string/#findComment-586570 Share on other sites More sharing options...
effigy Posted July 10, 2008 Share Posted July 10, 2008 Sure thing. See the Resources post. Personally, I fancy http://www.regular-expressions.info. Quote Link to comment https://forums.phpfreaks.com/topic/114111-solved-find-and-replace-within-a-string-maintaining-beginningend-of-said-string/#findComment-586614 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.