HTML标签的递归内容使用正则表达式(Recursive Contents of HTML tag using regex)

编程入门 行业动态 更新时间:2024-10-27 07:31:26
HTML标签的递归内容使用正则表达式(Recursive Contents of HTML tag using regex)

我正在为我的客户编写一个应用程序,该应用程序使用WYSIWYG来允许员工修改带有某些变量的信件模板,这些变量将被解析为信件所写信给客户的信息。

所见即所得生成的HTML可以保存到SQL服务器数据库中。 然后我使用一个PHP类来生成带有模板文本的PDF文档。

这是我的问题。 PDF生成类可以翻译b,u,i HTML标签。 而已。 除了我还需要翻译blockquote之外,这大部分都是可以的。 我认为最好的解决方案是编写一个正则表达式语句来获取每个blockquote HTML块的内容,并用五个空格替换块中的每一行。 诀窍是一些块引用可能包含嵌套的块引用(双缩进,而不是什么)

但不幸的是,我对于正则表达式从来没有太熟练,我花了1.5个小时用不同的模式进行实验,没有任何工作。

这里是侏儒:

字符串可能包含或不包含blockquote块 字符串可能包含多个blockquotes 字符串可能包含任何级别的blockquotes块的嵌套 我们可以依靠正确构建的HTML

一个示例输入字符串将看起来像这样:

Dear Charlie,<br><br>We are contacting you because blah blah blah blah.<br><br><br>To login, please use this information:<blockquote>Username: someUsername<br>Password: somePassword</blockquote><br><br>Thank you.

为了简单的解决方案,我需要用5个空格替换每个blockquote中的每个HTML分隔符,然后换行\ n换行符。

I'm writing an application for my client that uses a WYSIWYG to allow employees to modify a letter template with certain variables that get parsed out to be information for the customer that the letter is written for.

The WYSIWYG generates HTML that I save to a SQL server database. I then use a PHP class to generate a PDF document with the template text.

Here's my issue. The PDF generation class can translate b,u,i HTML tags. That's it. This is mostly okay, except I need blockquote to be translated too. I figure the best solution would be to write a regex statement that is to take the contents of each blockquote HTML block, and replace each line within the block with five spaces. The trick is that some blockquotes might contain nested blockquotes (double indenting, and what not)

But unfortunately I have never been too well versed with regex, and I spent the last 1.5 hours experimenting with different patterns and got nothing working.

Here are the gotchyas:

String may or may not contain a blockquote block String could contain multiple blockquotes String could contain potentially any level of nesting of blockquotes blocks We can rely on the HTML being properly formed

A sample input string would be look something like something like this:

Dear Charlie,<br><br>We are contacting you because blah blah blah blah.<br><br><br>To login, please use this information:<blockquote>Username: someUsername<br>Password: somePassword</blockquote><br><br>Thank you.

To simply the solution, I need to replace each HTML break inside each blockquote with 5 spaces and then the \n line break character.

最满意答案

你可能想检查一下PHP Simple HTML DOM Parser 。 您可以使用它来解析HTML DOM树的输入并使用它。

You might want to check PHP Simple HTML DOM Parser out. You can use it to parse the input to an HTML DOM tree and use that.

更多推荐

本文发布于:2023-07-17 07:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141288.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   标签   内容   正则表达式   HTML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!