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

Link to comment
Share on other sites

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'

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.