简体   繁体   English

PHP 将 HTML 转换为原始但保留断线

[英]PHP convert HTML to raw but keep breaklines

My project was running smoothly pushing the value of a <textarea> into PHP and the content published in social media (facebook, twitter) was exactly like the user wrote in the <textarea> .我的项目运行顺利,将<textarea>的值推入 PHP 并且在社交媒体(facebook、twitter)上发布的内容与用户在<textarea>中所写的完全一样。

Recently the client decided to implement mentions and in order for this to work I had to convert the <textarea> into a <div contenteditable="true"> and instead of sending the value to PHP I started sending the HTML.最近,客户决定实施提及,为了实现这一点,我必须将<textarea>转换为<div contenteditable="true">而不是将值发送到 PHP 我开始发送 HTML。 Needless to say this is where the headaches started.不用说,这就是令人头疼的地方。

Written in the contenteditable:写在contenteditable中:

One more test 👍
Lets go! www.google.com
#testone #testtwo

Sent from JS to PHP:从 JS 发送到 PHP:

One more test&nbsp;<span>👍</span><div><span>Lets go! www.google.com</span></div><div><span>#testone #testtwo</span></div>

PHP parsed & saved in DB: PHP 解析并保存在数据库中:

$text = preg_replace('#\<(.+?)\>#', ' ', $text);
$text = str_replace('&nbsp;', ' ', $text);

Value stored in DB:存储在数据库中的值:

One more test
 👍   Lets go! www.google.com    #testone #testtwo

As you can compare, the value stored in DB & the value initially written in the contenteditable is not the same, thus my problem.正如您可以比较的那样,存储在 DB 中的值和最初写入contenteditable中的值不一样,因此是我的问题。 The contenteditable has the css white-space: pre-line; contenteditable有 css white-space: pre-line; . .

The breaklines are not being stored properly and sometimes neither the space between words.特征线没有正确存储,有时单词之间的空格也没有。 How can I improve my PHP parse?如何改进我的 PHP 解析?

update:更新:

You can use the strip_tags function to delete tags您可以使用strip_tags function 删除标签

Example:例子:

$text = strip_tags('One more test&nbsp;<span>👍</span><div><span>Lets go! www.google.com</span></div><div><span>#testone #testtwo</span></div>');
echo $text;

Result:结果:

One more test&nbsp;👍Lets go! www.google.com#testone #testtwo                                                                                                               

Old:老的:

You can use the htmlentities function to store in the database您可以使用htmlentities function 存储在数据库中

And then use to display the same text然后用 来显示相同的文字

Example:例子:

$text = htmlentities('One more test&nbsp;<span>👍</span><div><span>Lets go! www.google.com</span></div><div><span>#testone #testtwo</span></div>');
echo $text;

Result:结果:

One more test&amp;nbsp;&lt;span&gt;👍&lt;/span&gt;&lt;div&gt;&lt;span&gt;Lets go! www.google.com&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;#testone #testtwo&lt;/span&gt;&lt;/div&gt;

Use on page:在页面上使用:

If you want only the text to be displayed in the same way:如果您只想以相同的方式显示文本:

 One more test&amp;nbsp;&lt;span&gt;&lt;/span&gt;&lt;div&gt;&lt;span&gt;Lets go. www.google;com&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;#testone #testtwo&lt;/span&gt;&lt;/div&gt;

If you want the code to run:如果您希望代码运行:

use html_entity_decode function to decode使用html_entity_decode function 进行解码

$result = html_entity_decode('One more test&amp;nbsp;&lt;span&gt;👍&lt;/span&gt;&lt;div&gt;&lt;span&gt;Lets go! www.google.com&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span&gt;#testone #testtwo&lt;/span&gt;&lt;/div&gt;');
echo $result;

Results on page:页面结果:

 One more test&nbsp;<span></span><div><span>Lets go. www.google.com</span></div><div><span>#testone #testtwo</span></div>

Solved .解决了

  1. Is stored in the DB with the breaklines与特征线一起存储在数据库中
  2. Is shown in the contenteditable with breaklines without the use of nl2br() function在不使用 nl2br() function 的情况下显示在带有折线的 contenteditable
  3. Is sent to Facebook & Twitter with the breaklines用隔断线发送到 Facebook & Twitter

When someone presses ENTER it creates a <div>....</div> , so I just replaced the <div> for the \n new line.当有人按下 ENTER 时,它会创建一个<div>....</div> ,所以我只是将<div>替换为\n新行。

$text = str_replace('<div>', "\n", $text);
$text = preg_replace('#\<(.+?)\>#', ' ', $text);
$text = str_replace('&nbsp;', ' ', $text);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM