2

I want to do the following:

I have a string in a database, it is stored with html tags when being inserted there, so for example, I might have the following string:

     <h2>Hello World</h2>
     <p>Cras mattis justo vitae diam sagittis ut porta eros aliquam. 
        Aenean vel nisi et nisl adipiscing blandit. 
        Donec tempor dictum risus a feugiat. 
        Nunc ac purus lectus. Morbi in suscipit ipsum. 
        Ut eu odio eu massa sollicitudin interdum. 
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
        Maecenas fermentum hendrerit imperdiet.</p>

This is a string I might have in the database, and I want to discard the <h2> part of it.

Now, as far as I know, doing a preg_replace() here, would do the job, but what string pattern do I need to look for?

Many thanks.

5
  • Parsing might be better then replacing: simplehtmldom.sourceforge.net Commented Jul 4, 2011 at 14:59
  • If your HTML is relatively simple, a regex might be able to cope with it. For general HTML, though you should look into using a parser instead of a regex to deal with it: stackoverflow.com/questions/1732348/… Commented Jul 4, 2011 at 14:59
  • @yes123: Don't assert; explain! Commented Jul 4, 2011 at 15:03
  • @tomalak: you stalk me really. anyway just to contradict once again DRY: don't use simplehtmldom use a proper xml parser not one based on strings. Commented Jul 4, 2011 at 15:05
  • @yes123: DRY, as a principle, is a great idea in a locality. But relying on DRY for something that you might have explained somewhere, once upon a time, in a far-away answer to a far-away question is pretty useless without providing an URL. Commented Jul 4, 2011 at 15:07

3 Answers 3

1

See the Remove HTML Tags section on the linked site. E.g.

@<h2[^>]*?>.*?</h2>@siu
Sign up to request clarification or add additional context in comments.

Comments

1

To actually answer your question the replace method RegEx would be:

/<h2>(.+?)<\/h2>/

However, as the comments pointed out, this isn't the best method! :)

Comments

1

Nice by Michael Wright.

Another way to solve this problem using strip_tags() function.

  $patern = '<p>';
  $str = '<h2>Hello World</h2><p>Cras mattis justo vitae diam .</p>';
  strip_tags($str,$patern);

This code generate the following output

 'Hello World<p>Cras mattis justo vitae diam .</p>'

Here $pattrn contain the tag list that still remain in you data. strip_tags() eliminates all tags except in $patern.

reference from php manual

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.