slushpuppie Posted October 29, 2009 Share Posted October 29, 2009 i string variable with some html in it. i want to be able to delete all code before "<div id="someName"> and after "<!-- end someName -->". so essentially just strip out a all the stuff around a certain div. any pointers? i saw something like this: /^[^@]+/i in another thread to get all stuff before the @ symbol... do i need something similar? replace the @ with my div??? Quote Link to comment https://forums.phpfreaks.com/topic/179521-trying-to-replace-parts-of-html-string-with-regex/ Share on other sites More sharing options...
cags Posted October 29, 2009 Share Posted October 29, 2009 Regular Expressions is probably not the best way to do this, you would probably be better off with DOMDocument or similar. But you could use a pattern like this to fetch the section you wish to keep, then override the original variable with it. You could use preg_replace if you wanted. '~(<div id="someName">.*?<!-- end someName -->)~is' Quote Link to comment https://forums.phpfreaks.com/topic/179521-trying-to-replace-parts-of-html-string-with-regex/#findComment-947278 Share on other sites More sharing options...
nrg_alpha Posted October 31, 2009 Share Posted October 31, 2009 i saw something like this: /^[^@]+/i in another thread to get all stuff before the @ symbol... do i need something similar? replace the @ with my div??? Simply replacing @ with div wouldn't help you at all. Reason being is that that character class (the part of the pattern containing [....]) checks for an individual character. So using [^div] means; at the current character being checked in the source string, check to see if it is not a d, i or v. You can learn more about regex in the following starter links: http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax http://www.regular-expressions.info/ http://weblogtoolscollection.com/regex/regex.php Obviously, googling regex tutorials will give plenty of additional options to sift through. Quote Link to comment https://forums.phpfreaks.com/topic/179521-trying-to-replace-parts-of-html-string-with-regex/#findComment-948362 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.