[英]How can I get 100 to 200 words from HTML without affecting HTML tags using Nodejs or Javascript?
I am getting data from my database and send in mail, data comes with HTML tags.我从我的数据库中获取数据并通过邮件发送,数据带有 HTML 标签。 Now problem is I want to display only 100 letters in mail.
现在的问题是我只想在邮件中显示 100 个字母。 But when I remove some words HTML tags also remove and it will destroy whole Output.
但是当我删除一些单词时,HTML 标签也会删除,它会破坏整个输出。 Please guide me how can I achieve this.
请指导我如何实现这一目标。
Sample Output From Database来自数据库的示例输出
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="snip">We are looking for physically fit individuals to fill general labour requirements by partnering with a truck driver. Your help is needed to load/unload the truck at the various job sites.
<b>Skills required: </b>
- comfortable with physical exertion and lifting minimum of 50lbs
- works well on a team but trusted to work independently
- reliable, self-motivated and committed to high standards of quality
- able to read and understand work instructions
<b>Specific requirements: </b>
- in good physical condition
- must have own safety footwear
- reliable transportation to ensure punctual and consistent attendance
If you meet the qualifications listed above, submit your resume in MS Word format via the link below.
<i>Previously employed with The Staffing Connection? Please contact our office to confirm your continued availability for these upcoming positions.</i></td>
</tr>
</tbody>
</table>
Required必需的
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="snip">We are looking for physically fit individuals to fill general labour requirements by partnering with a truck driver. Your help is needed to load/unload the truck at the various job sites.
<b>Skills required: </b>
- comfortable with physical exertion and lifting minimum of 50lbs
- works well on a team but trusted to work independently
- reliable, self-motivated and committed to high standards of quality
- able to read and understand work instructions
<b></b>
<i></i></td>
</tr>
</tbody>
</table>
I use something like 'Hiya how are you'.substring(0,8);
我使用类似
'Hiya how are you'.substring(0,8);
You cat use cheerio
libary for its:你的猫使用
cheerio
libary作为它的:
const cheerio = require('cheerio');
const input = `
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="snip">We are looking for physically fit individuals to fill general labour requirements by partnering with a truck driver. Your help is needed to load/unload the truck at the various job sites.
<b>Skills required: </b>
- comfortable with physical exertion and lifting minimum of 50lbs
- works well on a team but trusted to work independently
- reliable, self-motivated and committed to high standards of quality
- able to read and understand work instructions
<b>Specific requirements: </b>
- in good physical condition
- must have own safety footwear
- reliable transportation to ensure punctual and consistent attendance
If you meet the qualifications listed above, submit your resume in MS Word format via the link below.
<i>Previously employed with The Staffing Connection? Please contact our office to confirm your continued availability for these upcoming positions.</i></td>
</tr>
</tbody>
</table>
`;
const result = cheerio.load(input.substring(0, 200), { xmlMode: true });
console.log(result.html());
Example: https://stackblitz.com/edit/js-wr4fez?file=index.js示例: https : //stackblitz.com/edit/js-wr4fez?file=index.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.