I want to put paragraph tags around any text items. It should therefore avoid tables and other elements. How do I do that? I guess it somehow can be made with preg_replace?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
Here are a couple of functions that should help you to do what you want to do:
The first of these, nl2p(), takes a string as an input and converts it to an array wherever there is a newline (
"\n"
) character. Then it goes through each element and if it finds one that isn't empty, it will wrap<p></p>
tags around it and add it to a string, which is returned at the end of the function.The second, nl2p_html(), is a more complicated version of the former. Pass an HTML file's contents to it as a string and it will wrap
<p>
and</p>
tags around any non-HTML text. It does this by exploding a string into an array where the delimiter is the<
character, which is the start of any HTML tag. Then, iterating through each of these elements, the code will look for the end of the HTML tag and take anything that comes after it into a new string. This new string will itself be exploded into an array where the delimiter is a newline ("\n"
). Looping through this new array, the code looks for elements that are not empty. When it finds some data, it will wrap it in paragraph tags and add it to an output string. When this loop is finished, this string will be added back onto the HTML code and this together will be amended to an output buffer string which is returned once the function has completed.tl;dr: nl2p() will convert a string to HTML paragraphs without leaving any empty paragraphs and nl2p_html() will wrap paragraph tags around the contents of the body of an HTML document.
I tested this on a couple of small example HTML files to make sure that spacing and other things don't ruin the output. The code that's generated by nl2p_html() may not be W3C-compliant, either, as it will wrap anchors around paragraphs and the like rather than the other way around.
Hope this helps.