PHP DOMDocument loadHTML doesn’t keep tabs/lines generated by <br />
, instead it merges them and removes the <br />
breaks.
I’m using the below code to slightly change the WordPress’ posts content. Everything works fine except for the fact that the lines of text are not echoed as they are, separately. Instead, they are echoed in a single line/tab, making the text hard to read.
For example, what I’d like to see is this:
Specifications:
Name: VR glasses
Type: virtual reality glasses
Model: for VRGPRO+
Lens type: blue coated lens
But what I see instead is:
Specifications:Name: VR glassesType: virtual reality glassesModel: for VRGPRO+Lens type: blue coated lens
I have no idea how to fix this. I’ve tried removing all the LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_PARSEHUGE | LIBXML_COMPACT | LIBXML_NOBLANKS
but the result is the same. I’ve also tried the indentation fixes from here, plus other similar posts. Still no luck. Please help.
Here’s the code from the WP theme:
$dom = new DOMDocument;
$dom->loadHTML(strip_tags(mb_convert_encoding(get_the_content(), 'HTML-ENTITIES', 'UTF-8'), '<img>,<div>,<table>,<thead>,<tbody>,<tfoot>,<tr>,<th>,<td>,<ul>,<ol>,<li>,<strong>,<em>,<h3>,<h4>,<h5>,<h6>,<p>'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_PARSEHUGE | LIBXML_COMPACT | LIBXML_NOBLANKS);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//@*[not(name()="src")]') as $attr) {
$attr->parentNode->removeAttribute($attr->nodeName);
}
$images = $dom->getElementsByTagName('img');
foreach ( $images as $image )
{
$properlink = rtrim(preg_replace ('//','https:',$image->getAttribute('src'),1),'/');
$image->setAttribute( 'alt', esc_attr( wp_trim_words( get_the_title(), 10,'') ) );
$image->setAttribute( 'data-src', $properlink );
$image->removeAttribute('src');
$image->setAttribute( 'style', 'width:100%;max-width:100%;' );
}
while (($node_list = $xpath->query('//*[not(*) and not(@*) and not(text()[normalize-space()])]')) && $node_list->length) {
foreach ($node_list as $node) {
$node->parentNode->removeChild($node);
}
}
echo $dom->saveHTML();
Update 1:
This is the input, in the WP post the
is present, although they’re all in one line:
Specifications:<br />Name: VR glasses<br />Type: virtual reality glasses<br />Model: for VRGPRO+<br />Lens type: blue coated lens<br />
Update 2:
Specifically allowing the BR in the strip_tags did’t help much:
$dom->loadHTML(strip_tags(mb_convert_encoding(get_the_content(), 'HTML-ENTITIES', 'UTF-8'), '<img>,<div>,<table>,<thead>,<tbody>,<tfoot>,<tr>,<th>,<td>,<ul>,<ol>,<li>,<strong>,<em>,<h3>,<h4>,<h5>,<h6>,<p>,<br>'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_PARSEHUGE | LIBXML_COMPACT | LIBXML_NOBLANKS);
- this breaks everything and no input is echoed (blank page)
$dom->loadHTML(strip_tags(mb_convert_encoding(get_the_content(), 'HTML-ENTITIES', 'UTF-8'), '<img>,<div>,<table>,<thead>,<tbody>,<tfoot>,<tr>,<th>,<td>,<ul>,<ol>,<li>,<strong>,<em>,<h3>,<h4>,<h5>,<h6>,<p>,<br/>'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_PARSEHUGE | LIBXML_COMPACT | LIBXML_NOBLANKS);
- this doesn’t break the page but still strips the BRs. Both
<br />
and<br/>
behave the same (ie still getting removed).
Update 3:
Following suggestions to simplify the code, I’ve tried the below without any improvement:
$content = get_the_content();
$dom = new DOMDocument();
$dom->loadHTML(
strip_tags($content, '<img><br><div><table><thead><tbody><tfoot><tr><th><td><ul><ol><li><strong><em><h3><h4><h5><h6><p>'),
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_PARSEHUGE | LIBXML_COMPACT | LIBXML_NOBLANKS
);
$html = $dom->saveHTML();
$html = str_replace('<br>', "<br>n", $html); // if you want newlines
echo $html;
Update 4:
As per request, this is what a typical post contains
<br /><p><br /></p><p></p><p></p><p><br /></p><br /><p><div><div><div id="294794216367" data-previewurl="//img.alicdn.com/imgextra/i1/6000000000970/O1CN01xXGd7M1J2Iq5QnJhY_!!6000000000970-0-tbvideo.jpg"></div></div></div></p><br /><p><br /></p><p><br /></p><div><p style="font-family:OpenSans;font-size:20px;font-weight:900;line-height:28px;margin-bottom:12px">We Support Drop Shipping!</p></div><div><p style="font-family:OpenSans;font-size:20px;font-weight:900;line-height:28px;margin-bottom:12px">VRG Pro+:</p><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Note: In order to give you 100% protection of your purchasing glasses ,we will update the shipping into air filled column bag pack like the photo.</p></div><div><img src="//ae01.alicdn.com/kf/H81b56447280d441c914cb64de1c17080Q.jpg" /></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">If you need the item with Retail Box, you can choose the "Glasses add Box" or "add Remote add Box ", and we will send you with the retail box like this photo :</p></div><div><img src="//ae01.alicdn.com/kf/He7c0b55eee974cbdbc16b4d5995034fds.jpg" /></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Features:<br />1. with A Large Headset: The VR glasses with a large headset provides a great experience. The sound quality is strong and stable, the large screen frame is detachable, both large and small screen phones are available (maximum support 7 inches screen phone).<br />2. Full-screen Visual Experience: The maximum image effect and overall experience enable you to enjoy wonderful movies as if you were sitting in front of the theater screen. There is no black-box effect when watching movies.<br />3. Enjoy the Visual Feast: There are independent adjusters for pupil and 0-800° myopia adjustment, so as to provide users with the best visual field. When wearing the goggles, 0-800° myopia users do not need to wear another pair of glasses.<br />4. High Transmittance Optical Lens: The blue-coated lenses can effectively prevent the influence of blue light, stray light and aura, thus ensuring a clearer 120° wide-angle field of view and better immersion.<br />5. Suitable for 5-7 Inch Smartphones: This pair of visual reality goggles is suitable for 5-7 inch IOS and for Android phones. Easily enjoy the perfect screen time.<br /><br />Specifications:<br />Name: VR glasses<br />Type: virtual reality glasses<br />Model: for VRGPRO+<br />Lens type: blue coated lens<br />Lens diameter: about 42mm/1.65in<br />Viewing angle: 120°<br />Myopia adjustment: 0-800°<br />Pupil adjustment: about 60-75mm/2.36-2.95in<br />Material: ABS<br />Compatible platforms: for IOS, for Android<br />Suitable for: 5-7 inch smart phone<br />Plug type: direct insertion<br />Wearing style: head-mounted<br />Headphones: built-in headphones<br />Size: about 19x10x11cm/7.48*3.94*4.33in<br /><br />Packing List:<br />1 x VR Glasses<br />1 x Lens Cloth<br />1 x User Manual<br /><br /><br />Headset Theater Version<br />VRGPRO+<br />New breakthrough in 2020<br />Cinematic audiovisual experience</p></div><div><img src="//ae01.alicdn.com/kf/Hf9376516a5894c93812c3ff2162b1272x.jpg" /></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">8 reasons to choose us<br />① Switch between phones<br />② Large speaker unit<br />③ Compatible with 5 to 7 inches smart phones<br />④ 120-degree FOV (wide angle)<br />⑤ VR glasses with full-screen vision<br />⑥ Adjustable interpupillary distance<br />⑦ Suitable for 0-800 degrees myopia<br />⑧ Blue light filter lenses</p><div><img src="//ae01.alicdn.com/kf/Hea09a6ed8b0e49508f761872ab00cef83.jpg" /></div></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Enjoy 3D films on your phone<br />Immersive visual and auditory experience</p><div><img src="//ae01.alicdn.com/kf/H8048c972841944acbcc43334d3e5febeI.jpg" /></div></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Big screen = more fun<br />Are you still buying a regular VR?<br />Or you bought a new phone only to find that it is not compatible with your VR?<br /> <br />Old VR glasses are not made for new phones.<br />Adopting the design for easy switch between small and large screens, it allows you to freely change between phones with different screen sizes.</p><div><img src="//ae01.alicdn.com/kf/Hc2b2ab25033c4b4cb1dde08cbf9440b2S.jpg" /></div></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Unfold the new era of 5G audio and video with new VR glasses (Compatible with both new and old phons)</p><div><img src="//ae01.alicdn.com/kf/H7d95a8402380426fadcbc6b86c24e7fe5.jpg" /></div></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Small or big, switch as you like.<br />Adopting the design for easy switch between small and large screens, it allows you to freely change between phones with different screen sizes.</p><div><img src="//ae01.alicdn.com/kf/H55a7104b05724c9881af8f8774995d6ax.jpg" /></div></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">VR glasses with full-screen vision <br />Full screen, no borders</p><div><img src="//ae01.alicdn.com/kf/H122113e8c26842e2b4b4d71fa056470fq.jpg" /></div></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Enjoy a shocking audio-visual feast<br />10 years of professional <br />research on speaker sound<br />quality to make products better<br />Incredibly immersive experience</p><div><img src="//ae01.alicdn.com/kf/H1c8e6e2541554065b484c1eaf7b85aa59.jpg" /></div></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">VR GLASSES+<br />HEADPHONES AIO<br />Hi-Fi Headphones<br /> <br />Independent control buttons<br />As is the case with most headphones, this headset may not match all<br />video/audio apps and their keypad functions<br />Sorry for the inconvenience brought <br />(ios devices accept only original Apple accessories in terms of volume control).</p><div><img src="//ae01.alicdn.com/kf/H9b92f76bfbc74bada6a5c292f5af05f9v.jpg" /></div></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Hi-Fi Headphones<br />Enjoy a shocking audio-visual feast</p><div><img src="//ae01.alicdn.com/kf/H4625ad2de05a4acca46bbcf211f64cf8S.jpg" /></div></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Easily embark on your 3D journey<br />1. Open the mobile phone APP;<br />2. Put the mobile phone into the glasses;<br />3. Insert the headphone plug into the mobile phone</p><div><img src="//ae01.alicdn.com/kf/H3d89e020199c484d9450526024f872b5W.jpg" /></div></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Compatible with 5 to 7 inches smart phones<br /> <br />Suitable for Android / iPhone<br />Adopting the design for easy switch between small and large screens, it allows you to freely change between phones with different screen sizes.</p><div><img src="//ae01.alicdn.com/kf/Hce79932eb4634fa8944fb709e8b7dd48C.jpg" /></div></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">3D effect + immersion</p><div><img src="//ae01.alicdn.com/kf/H77d6fc0f9276410bb1e74cb6ba175f9d4.jpg" /></div></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Friendly to myopia users within 800 degrees<br />No need to wear myopia glasses (Adjustment of the VR glasses will do.)</p><div><img src="//ae01.alicdn.com/kf/H33133a5d6dba44f3b00059f04246d5c3H.jpg" /></div></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Interpupillary distance adjuster</p><div><img src="//ae01.alicdn.com/kf/Hb97d5d335808435d83bde4e2d376ef66K.jpg" /></div></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Blue light filter lenses</p></div><div><img src="//ae01.alicdn.com/kf/Hcd4ae33bc27347b08af5b3b75810693eE.jpg" /><img src="//ae01.alicdn.com/kf/H940cdee1dfce4fdeb50311df2b729c3fR.jpg" /><img src="//ae01.alicdn.com/kf/H9bccd755eb7f4102917d0c49654c455cX.jpg" /></div><div><p style="font-family:OpenSans;font-size:20px;font-weight:900;line-height:28px;margin-bottom:12px">VRG Pro:</p><div><img src="//ae01.alicdn.com/kf/Se2b847e866dd42cfbc29604f4f192f96W.jpg" /><img src="//ae01.alicdn.com/kf/S9cb212bb714a474eb7a20eb862cd8d007.jpeg" /><img src="//ae01.alicdn.com/kf/Sc7427707658e447a918768d3c29f8dd57.jpeg" /><img src="//ae01.alicdn.com/kf/S5c6fff82027947cfa1cd45d9d5b41f6ee.jpeg" /><img src="//ae01.alicdn.com/kf/S36bc92ec73d748fe8e9c1ddca033fa3bA.jpeg" /><img src="//ae01.alicdn.com/kf/S18cfd313885d4c6b9cead56c7697e6f2A.jpeg" /><img src="//ae01.alicdn.com/kf/S9f953d176a444af8aeaad4da60180056J.jpeg" /><img src="//ae01.alicdn.com/kf/S806f6ea741994b08870b0433748fc6aah.jpeg" /><img src="//ae01.alicdn.com/kf/Sbbef574825164adfb49529e216e5690f7.jpeg" /><img src="//ae01.alicdn.com/kf/Sd9e3cd9c325c4861a3cb4d0e89980149N.jpg" /></div></div><div><img src="//ae01.alicdn.com/kf/S644048b567a4403d823d908b285a9869S.jpg" /><img src="//ae01.alicdn.com/kf/S4c451fcd3e444f95a9fb7a6caf3716fc7.jpg" /><img src="//ae01.alicdn.com/kf/S7dfb611595cc460ba8a272abf414537cj.jpg" /><img src="//ae01.alicdn.com/kf/S3bf548f4d39d4f8b9ab1278bb21d3670w.jpg" /><img src="//ae01.alicdn.com/kf/Sbf3eb629fa624305bf19b8a5a6300f9d5.jpg" /><img src="//ae01.alicdn.com/kf/S24e7ed14f71c4413852dfe545e80073f1.jpg" /><img src="//ae01.alicdn.com/kf/Sd011a012dd68454fa9b807f2a8ef39e5d.jpg" /><img src="//ae01.alicdn.com/kf/S721716f4485e4a3b80bdbc1da5ac2490G.jpg" /><img src="//ae01.alicdn.com/kf/Sdaa0d09b6f9144deb76980ff681089b7q.jpg" /><img src="//ae01.alicdn.com/kf/Sd765d44587f84c33a89906c7f590cd65n.jpg" /></div><div><p style="font-family:OpenSans;font-size:20px;font-weight:900;line-height:28px;margin-bottom:12px">Remote Control 1 : VR PARK Y1</p></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Support for IOS, Android Dual System<br />it can play all the games which needn't touch<br />Support Android Games, IOS jailbreak games</p></div><div><img src="//ae01.alicdn.com/kf/H08b0a90aa1ad4ae9ad5c451842de3017m.jpg" /><img src="//ae01.alicdn.com/kf/H3d04bf8363ea48518ad9cbf8de7ae94eK.jpg" /></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Product Parameters<br />360° Remote Control<br />Mouse mode / Camera<br />Switch<br />A Button<br />B Button<br />C Button<br />D Button</p></div><div><img src="//ae01.alicdn.com/kf/Hcd0d72735a96477999aa8130d97bcdd5W.jpg" /><img src="//ae01.alicdn.com/kf/H2a1f6ac5afed4bbfb5dc171bf0297d99T.jpg" /></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">2-10 meters wireless range<br />Completely innovative design, complete innovative technology</p></div><div><img src="//ae01.alicdn.com/kf/Hfb3f3dd5d5a94f5e9df209a078377f12h.jpg" /></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Superior Compatibility and Functionality<br />Can be used as mobile phone games handle, handle your tablet,<br />PC computer remote control, wireless Bluetooth handle <br />European, wireless mouse, music control buttons, VR helmets games, support for Apple (IOS) system, Android systems.</p></div><div><img src="//ae01.alicdn.com/kf/H97f8df60b3d94ac5a909468bc00f8b80a.jpg" /></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Delicate touch ultimate control<br />Ergonomic design, the slightest fit players hands, <br />Perfect grip experience the ultimate fun of the game</p></div><div><img src="//ae01.alicdn.com/kf/H7f72e067937544acb845c2af9550bfadd.jpg" /><img src="//ae01.alicdn.com/kf/H6ffe810a1635454fa2e4f2553c49c8e0w.jpg" /><img src="//ae01.alicdn.com/kf/H7ff92671f52141a29b0e2988254b5812w.jpg" /></div><div><p style="font-family:OpenSans;font-size:20px;font-weight:900;line-height:28px;margin-bottom:12px">Remote Control 2 : Ipega 9156</p></div><div><img src="//ae01.alicdn.com/kf/Se872af85acd6449c9dfc07295865e21fL.jpg" /><img src="//ae01.alicdn.com/kf/S2761918099cc43c4924da3c857ea9364X.jpg" /><img src="//ae01.alicdn.com/kf/Sbe9506a14b2240a0b4f9513687ab5d3el.jpg" /><img src="//ae01.alicdn.com/kf/S124d137d4fb5432e8747ef54e9894e719.jpg" /><img src="//ae01.alicdn.com/kf/S6a115b2b06254d9888c7fe8e5b72fc22W.jpg" /><img src="//ae01.alicdn.com/kf/Sab6d08b3e2354c2d9a2af90f51312d18E.jpg" /><img src="//ae01.alicdn.com/kf/Se6acc54ccb504e6ab9206b033f1a2404C.jpg" /><img src="//ae01.alicdn.com/kf/S9480722b7a2e413abe393ad7413e26802.jpg" /><img src="//ae01.alicdn.com/kf/Sf62dc3dc67f14eada4289500985afdceM.jpg" /><img src="//ae01.alicdn.com/kf/S4e04bbf315fa403aa6691efd922a4308F.jpg" /></div><div><img src="//ae01.alicdn.com/kf/S1182fcec60b04e508e67e6f45e62ba64l.png" /><img src="//ae01.alicdn.com/kf/S311818b663b045cab8a612ca30e685d3X.png" /><img src="//ae01.alicdn.com/kf/Sf2cf1c8e510c44a48cd1928d0370ab00U.png" /></div><div><p style="font-family:OpenSans;font-size:20px;font-weight:900;line-height:28px;margin-bottom:12px">Remote Control 3: MOCUTE 054</p><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Button Description</p></div><div><img src="//ae01.alicdn.com/kf/H0a276f6fc802403dac387dff55ce75e1T.jpg" /></div><div><img src="//ae01.alicdn.com/kf/Hd7b1f3bc00ee4d9b86783aeb1ac9277bz.jpg" /></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">FULLY COMPATIBLE<br />SUPPORT A VARIETY OF SYSTEMS<br />BUILT-IN LITHIUM BATTERY<br />BLUETOOTH WIRELESS CONNECTION<br />DON'T NEED TO CRACKED THE IOS SYSTEM<br />DON'T NEED TO ROOT<br />SUPPORT VR VIDEO CONTROL<br />MUSIC CONTROL<br />WIRELESS OPERATION</p></div><div><img src="//ae01.alicdn.com/kf/H1dd26407f2354c36b36197173d9acab6n.jpg" /></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Automatically ldentify IOS / Android system<br />Do not need to crack the phone system<br />Supports IOS/Android /PC/Table PC</p></div><div><img src="//ae01.alicdn.com/kf/H3171959e77484caea2740b3caa6205b3z.jpg" /></div><div><img src="//ae01.alicdn.com/kf/H0a276f6fc802403dac387dff55ce75e1T.jpg" /></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Ergonomics Design Handle</p></div><div><img src="//ae01.alicdn.com/kf/H92c51a746d0746d9be696394277f0e9fc.jpg" /></div><div><img src="//ae01.alicdn.com/kf/H2595deeb0cda437c942d4ccd79f50dc2X.jpg" /></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">Sensitive key design 256 segments sensor<br />Crystal material Independent key</p></div><div><img src="//ae01.alicdn.com/kf/H1d1a1c3be1c84325ab883aafcecac7252.jpg" /></div><div><p style="font-family:OpenSans;font-size:14px;font-weight:300;line-height:20px;margin-bottom:12px">High precision Joystick<br />With Phone Holder</p></div><div><img src="//ae01.alicdn.com/kf/H390cea86b30042cbab8ed0fe4f3bfc4d6.jpg" /></div><div><img src="//ae01.alicdn.com/kf/H1d1a1c3be1c84325ab883aafcecac7252.jpg" /></div><div></div><div><img src="//ae01.alicdn.com/kf/H9660bf669a2b47c194ea5902dbf06d99B.jpg" /></div><p><br /></p><p><br /></p><p><br /></p><br />
2
Answers
A bunch of notes on your (or your theme’s) code — and a simple solution for the result you want.
Syntax Issue: No comma required between allowed tags for strip_tags (although it seems to work); use
<a><br>
string format, or an array (since PHP 7.4). Also, "Self-closing XHTML tags are ignored and only non-self-closing tags should be used" (always check the manual).Observation:
<br>
tags are stripped even with a straight$dom->loadHTML($content);
in your script. That’s why I was urging you to test withoutstrip_tags
— to reduce code and help narrow down the actual source of your problem.Note: that
mb_convert_encoding
is deprecated. Assuming content in your database is in some flavor of UTF-8, this is unnecessary. You can use html_entity_decode() to convert entities.Debugging: Move
echo $dom->saveHTML(); exit;
down the code to see where your<br>
tags actually disappear. They’re intact if I echo out the HTML right after$dom->loadHTML()
.Discovery: Breaks disappear after your
while (($node_list = $xpath->query
loop that removes any and all children. (Not sure why that code is there to begin with.)Solution: You don’t need most of that code to transform your given input to your desired output. All you need is this:
Result (without and with addition of newlines after
<br>
tags):If you don’t want the
<br>
HTML tags, use$html = str_replace('<br>', "n", $html);
to replace HTML line breaks with the newline characters (n
). Note that DOMDocument gives the result in a<p>
node. Use e.g.strip_tags
to remove them.Update: Given your complete sample data and desired output, if what you are really hoping to accomplish is simply to keep the plain text content with linebreaks added — then there’s a much simpler way to do that, without the need to use
DOMDocument
or other HTML parsers.This gives you a tidy plain text version of your post with newlines added, tags removed and space compacted. See demo
While this code yields an OK result for your sample content without fine-tuning, you could target specific tags for the
n
newline addition for your one-line HTML bulk. Specifically, you’d want to target block level elements and not add newlines after inline elements likespan
,b
,i
.You know I read all the answers above and sadly it is so tricky to fix it using PHP, now you can do a very simple thing to fix it, WordPress writes with a visual editor by default even if you use Classic editor, so comes
<br />
instead of<br>
automatically. I am gonna give you two method-METHOD 1:-
Install Real-Time Find and Replace plugin. Now go to Tool > Real-Time Find and Replace > add
<br />
inFind:
field and add<br>
inReplace with
field. See now it is fixed.METHOD 2:
Use the block editor that comes by default with WordPress if still not fixed then use custom HTML to create your post and your problem will be solved. If you’ve written a lot of posts, you can export your entire post to an XML file. Now edit the XML file.
<!-- wp:html -->
before start your content just after<content:encoded><![CDATA[
<!-- /wp:html -->
after end your content just before]]></content:encoded>
<br />
tag with<br>
.If you are using any advance code editor in your device then you know how to do all these things in just one click. ie: visual studio, sublime text. hint- CTRL + F -> type what you looking for -> ALT + Enter -> Write new code. basically it selects multiple lines in one time.
now click on save
Now you need to permanently delete your all post. You can just use WP BULK DELETE plugin. Once you have deleted all of them now import your XML file that you edited. You can see any video on youtube and read blogs if you have never used import & export. After importing you can see that your post has been successfully imported.
Disclaimer:-