开发者

PHP Preg_Replace data between tags, respecting other tags within the document

开发者 https://www.devze.com 2023-02-18 11:01 出处:网络
There is probably a very simple answer to this, but I want to be as detailed as possible so that you do not need me to clarify.

There is probably a very simple answer to this, but I want to be as detailed as possible so that you do not need me to clarify.

I am trying to collect the contents of every

<content><div>CONTENT</div></content>

The content needs to be returned as a backreference ($1). Both the content and the div have differing parameters (such as style="color: white;"). These parameters are unimportant, but exist nonetheless.

The complication is that the div may contain child div's. These are not important, but conflict with my current regex - stopping the match early.

Here is a sample of the code, imagine this copy/pasted several times and formatted differently.

<entry> 
<title>A general title of a post</title> 
<content type="xhtml"> 
    <div xmlns="http://www.w3.org/1999/xhtml"> 
    This is a description of the title. It may <b>contain bold text</b> or <div>even divs</div>, and everything else. It is not quite important to save these tags, but they exist nonetheless.
    </div> 
</content> 
</entry>

Currently, I am using two regex codes. One for the declaration, and one for the closing tags. This works, but now I need to execute code on the contents. So, I will use preg_replace_callback(), but I can't figure out how to connect the two so that the middle is a callback.

Declaration:

<content \w+\s*=\s*\".*?\">[\r\n\s]{0,}<div \w+\s*=\s*\".*?\">

Closing:

</div>[\r\n\s]{0,}</content>

I need these combined, with the contents returned as a callback. I have tried something like ([\w\W]{0,})开发者_Go百科, which returns absolutely everything, but this match doesn't stop at the closing div.

So I found out about the \bFULLWORD\b command, and threw \bdiv\b on that... But I have had no success getting that to work. Perhaps it is not supported by PHP? Or I am stupid.

I do not know.

Please help!


It's been said before and it's being said now, and unfortunately it's going to be said again. Regex is a wonderful tool. It's great for manipulating strings and pattern matching of regular expressions.

HTML is not a string. HTML is a markup language, not a regular language. It's not truthfully a string, but can be interpreted as one (and thus, why we can technically use regex to manipulate HTML). HTML is it's own language based on element nodes, you need to manipulate those elements if you're going to change something.

As pointed out in the comments, you can easily use the DOM class in PHP.

You want to do this for quite a few reasons:

  • It's easier, you don't need to make some crazy pattern that looks like a cat walked across your keyboard
  • It's easier (again), you can navigate to the specific node, not work with the whole document.
  • It's safer, you don't accidentaly change something you didn't want to
  • It's safer (again), the source data can change, and you can detect it and account for it.
  • It's safer (again again), you can fail gracefully.

How?

  • how to use dom php parser
  • http://php4every1.com/tutorials/php-domdocument-tutorial/
  • Check the manual linked to above.
  • Just google for it. You already learned regex, and this is significantly less complicated.


Use a DOM parser. Here's an example: http://htmlparsing.com/php.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号