提问人:Johnny Bravo 提问时间:5/27/2023 最后编辑:Johnny Bravo 更新时间:6/1/2023 访问量:128
PHP DOM文档加载HTML在Wordpress帖子内容中不保留<BR />行 - 如何修复?
PHP DOMDocument loadHTML not preserving <BR /> lines in Wordpress posts content - how to fix?
问:
PHP DOMDocument loadHTML 不保留 生成的制表符/行,而是合并它们并删除换行符。
我正在使用以下代码稍微更改 Wordpress 的帖子内容。一切正常,除了文本行没有单独回显。相反,它们在一行/制表符中回显,使文本难以阅读。<br />
<br />
例如,我想看到的是这样的:
Specifications:
Name: VR glasses
Type: virtual reality glasses
Model: for VRGPRO+
Lens type: blue coated lens
但我看到的是:
Specifications:Name: VR glassesType: virtual reality glassesModel: for VRGPRO+Lens type: blue coated lens
我不知道如何解决这个问题。我试过删除所有,但结果是一样的。我还尝试了这里的缩进修复,以及其他类似的帖子。还是没有运气。请帮忙。LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_PARSEHUGE | LIBXML_COMPACT | LIBXML_NOBLANKS
以下是 WP 主题的代码:
$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();
更新1:
这是输入,在 WP 帖子中存在
,尽管它们都在一行中:
Specifications:<br />Name: VR glasses<br />Type: virtual reality glasses<br />Model: for VRGPRO+<br />Lens type: blue coated lens<br />
更新2:
具体来说,允许 BR 在strip_tags中并没有多大帮助:
$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);
- 这会破坏所有内容,并且不会回显任何输入(空白页)
$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);
- 这不会破坏页面,但仍然会剥离 BR。两者的行为相同(即仍然被删除)。
<br />
<br/>
更新3:
按照简化代码的建议,我尝试了以下方法,没有任何改进:
$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;
更新4:
根据要求,这是典型帖子包含的内容
<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 />
答:
关于你(或你的主题)代码的一堆注释 - 以及你想要的结果的简单解决方案。
语法问题:strip_tags允许的标签之间不需要逗号(尽管它似乎有效);使用字符串格式或数组(从 PHP 7.4 开始)。此外,“自闭合 XHTML 标记将被忽略,只应使用非自闭合标记”(请务必查看手册)。
<a><br>
观察:即使在脚本中使用直音,标签也会被剥离。这就是为什么我敦促你不进行测试 -- 以减少代码并帮助缩小问题的实际根源。
<br>
$dom->loadHTML($content);
strip_tags
注意:已弃用。假设数据库中的内容是某种 UTF-8 风格,这是不必要的。您可以使用 html_entity_decode() 来转换实体。
mb_convert_encoding
调试:向下移动代码以查看标记实际消失的位置。如果我在 HTML 之后立即回显 HTML,它们就完好无损。
echo $dom->saveHTML(); exit;
<br>
$dom->loadHTML()
发现:在删除所有子项的循环后,中断会消失。(不知道为什么一开始就有这段代码。
while (($node_list = $xpath->query
解决方案:您不需要大部分代码即可将给定的输入转换为所需的输出。您所需要的只是这个:
$content = 'Specifications:<br />Name: VR glasses<br />Type: virtual reality glasses<br />Model: for VRGPRO+<br />Lens type: blue coated lens<br />';
$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;
结果(在标记后不添加换行符和添加换行符):<br>
<p>Specifications:<br>Name: VR glasses<br>Type: virtual reality glasses<br>Model: for VRGPRO+<br>Lens type: blue coated lens<br></p>
<p>Specifications:<br>
Name: VR glasses<br>
Type: virtual reality glasses<br>
Model: for VRGPRO+<br>
Lens type: blue coated lens<br>
</p>
如果您不需要 HTML 标记,请使用换行符 () 替换 HTML 换行符。请注意,DOMDocument 在节点中给出结果。使用例如 以删除它们。<br>
$html = str_replace('<br>', "\n", $html);
\n
<p>
strip_tags
更新:给定完整的示例数据和所需的输出,如果您真正希望完成的只是保留添加换行符的纯文本内容,那么有一种更简单的方法可以做到这一点,而无需使用或其他 HTML 解析器。DOMDocument
$content = html_entity_decode($content); // decode entities
$content = str_replace(">", ">\n", $content); // add line-breaks
$content = strip_tags($content); // remove all tags
$content = trim($content); // trim leading/trailing space
$content = preg_replace('~\v{2,}~', "\n\n", $content); // compact multiple vertical spaces
echo $content;
这为您提供了整洁的纯文本版本的帖子,添加了换行符,删除了标签并压缩了空间。观看演示
虽然此代码在不进行微调的情况下为示例内容生成 OK 结果,但您可以针对单行 HTML 批量添加换行符的特定标记。具体来说,您希望以块级元素为目标,而不是在内联元素(如 、 、 等)后添加换行符。\n
span
b
i
评论
Specifications:<br />Name: VR glasses<br />Type: virtual reality glasses<br />Model: for VRGPRO+<br />Lens type: blue coated lens<br />
<br />
get_the_content()
strip_tags
while
<br>
<br>
<br>
你知道我阅读了上面的所有答案,可悲的是,使用 PHP 修复它非常棘手,现在你可以做一件非常简单的事情来修复它,即使你使用经典编辑器,WordPress 也默认使用可视化编辑器编写,所以来而不是自动。我给你两个方法——<br />
<br>
方法1:-
安装实时查找和替换插件。现在转到工具>实时查找和替换>在查找字段中
添加 <br />
,然后在替换为
字段中添加 <br>
。现在看到它已修复。
方法2:
使用 WordPress 默认附带的块编辑器,如果仍未修复,则使用自定义 HTML 创建您的帖子,您的问题将得到解决。如果您写了很多帖子,则可以将整个帖子导出为XML文件。现在编辑 XML 文件。
- 在开始内容之前添加
<!-- wp:html -->
<content:encoded><![CDATA[
- 在结束之前结束内容后添加
<!-- /wp:html -->
]]></content:encoded>
- 现在将所有标签替换为 .
<br />
<br>
如果您在设备中使用任何高级代码编辑器,那么您只需单击一下即可知道如何完成所有这些事情。即:Visual Studio,崇高的文本。 提示 - CTRL + F ->键入您要查找的内容 -> ALT + Enter -> 编写新代码。 基本上,它一次选择多行。
现在点击保存
现在,您需要永久删除所有帖子。您可以只使用 WP BULK DELETE 插件。删除所有这些文件后,现在导入您编辑的 XML 文件。如果您从未使用过导入和导出,您可以在 youtube 上观看任何视频并阅读博客。导入后,您可以看到您的帖子已成功导入。
免责声明:-
- 在删除帖子之前,必须备份您的网站。
- 如果您已经安装了 Starter Templates — Elementor、WordPress 和 Beaver Builder Templates 插件,只需在导入之前停用它,因为它与 WordPress 导入不兼容,因为它在导入时会在您的帖子中添加一些不寻常的字符。如果仍然出现特殊字符,那么您可以停用所有插件并再次导入,它肯定会起作用。
评论
strip_tags
<br>
strip_tags
$dom->preserveWhiteSpace=true;