Jump to content

trying to replace parts of html string with regex...


slushpuppie

Recommended Posts

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???

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'

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.