使用php将HTML输出转换为纯文本 [英] Convert HTML output into a plain text using php
问题描述
我试图将我的示例HTML输出转换为纯文本,但我不知道如何。我使用file_get_contents,但我试图转换的页面最像返回一样。
$ raw =http://localhost/guestbook/profiles.php;
$ file_converted = file_get_contents($ raw);
echo $ file_converted;
profiles.php
< HTML>
< head>
< title>个人资料 - 留言簿< /标题>
< link rel =stylesheettype =text / csshref =css / style.css>
< / head>
< body>
<! - 有些Divs - >
< div id =profile-wrapper>
< h2>个人资料< / h2>
< table>
< tr>
< td>名称:< / td>< td> John Dela Cruz< / td>
< / tr>
< tr>
< td>年龄:< / td>< td> 15< / td>
< / tr>
< tr>
< td>位置:< / td>< td> SomewhereIn,亚洲< / td>
< / tr>
< / table>
< / div>
< / body>
< / html>
基本上,我试图回应出类似这样的内容(纯文本,没有样式)
个人资料
姓名:John Dela Cruz
年龄:15
地点:某处在亚洲
但我不知道如何。 :-(。请帮助我,预先感谢你。
编辑:因为我只是在页面的内容之后,不管它是风格还是只是一个纯文本,有没有一种方法可以使用file_get_contents()来只选择(见下面的代码)?
< h2> Profile< ; / h2>
< table>
< tr>
< td>名称:< / td>< td> John Dela Cruz< / td>
< / tr>
< tr>
< td>年龄:< / td>< td> 15< / td>
< / tr>
< tr>
< td>位置:< / td>< td> SomewhereIn,Asia< / td>
< / tr>
< / table>
使用php strip_tags
如果strip_tags不适用于m您可以使用正则表达式来提取您想要的信息。
尝试使用PHP preg_match 与 /(< td>。*?<\\ / td>)/
模式
I'm trying to convert my sample HTML output into a plain text but I don't know how. I use file_get_contents but the page which I'm trying to convert returns most like the same.
$raw = "http://localhost/guestbook/profiles.php";
$file_converted = file_get_contents($raw);
echo $file_converted;
profiles.php
<html>
<head>
<title>Profiles - GuestBook</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<!-- Some Divs -->
<div id="profile-wrapper">
<h2>Profile</h2>
<table>
<tr>
<td>Name:</td><td> John Dela Cruz</td>
</tr>
<tr>
<td>Age:</td><td>15</td>
</tr>
<tr>
<td>Location:</td><td> SomewhereIn, Asia</td>
</tr>
</table>
</div>
</body>
</html>
Basically, I trying to echo out something like this (plain text, no styles)
Profile
Name: John Dela Cruz
Age: 15
Location: SomewhereIn, Asia
but i don't know how. :-( . Please help me guys , thank you in advance.
EDIT: Since i am only after of the content of the page, no matter if it's styled or just a plain text , is there a way to select only (see code below) using file_get_contents() ?
<h2>Profile</h2>
<table>
<tr>
<td>Name:</td><td> John Dela Cruz</td>
</tr>
<tr>
<td>Age:</td><td>15</td>
</tr>
<tr>
<td>Location:</td><td> SomewhereIn, Asia</td>
</tr>
</table>
Use php strip_tags
If strip_tags is not working for then maybe you can use regex to extract the info you want.
Try using PHP preg_match with /(<td>.*?<\/td>)/
as the pattern
这篇关于使用php将HTML输出转换为纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!