I am trying to get some data out from a database stored as text usnig php. The text could be plain text /or html text.
我试图从存储为文本usnig php的数据库中获取一些数据。文本可以是纯文本/或纯文本。
I want to fetch the following fields from the text retrieved from database : City, state, country. Here is the example the text could look like
我想从数据库中检索的文本中获取以下字段:城市,州,国家。这是文本的样子
country :- US
state :- Washington
city :- Bellingham
or
<table>
<tr><td>country </td><td>US</td></tr>
<tr><td>state</td><td>Washington</td></tr>
<tr><td>city</td><td>Bingen</td></tr>
</table>
or same in div tag
或者在div标签中相同
I have already done for plain text. which I found to be very easy as i found the linebreak (the end of line) and trimmed rest of the text except what was required
我已经为纯文本做过了。我发现这很容易,因为我找到了换行符(行尾)并修剪了其他文本,除了要求的内容
But in case of HTML I am facing the problem, how to deal with html text. As there is no line break . Not sure whats the best way to do this if I use strip_tags it returns all text in same line i.e. country us state washington
但是在HTML的情况下,我面临着问题,如何处理HTML文本。因为没有换行符。不知道什么是最好的方法,如果我使用strip_tags它返回同一行中的所有文本,即country us state washington
2 个解决方案
#1
2
Can't you just do some text processing instead of HTML parsing
你不能只做一些文本处理而不是HTML解析
$input = "<table><tr><td>country </td><td>US</td></tr><tr><td>state</td><td>Washington</td></tr><tr><td>city</td><td>Bingen</td></tr></table>";
$needle = array("<table>", "<tr><td>","</td<td>", "</td</tr>", "</table");
$replacements = array("","",":-","\n","");
$output = str_replace($needle, $replacements, $input);
This will give you the same as the text-input.
这将为您提供与文本输入相同的功能。
#2
2
When you are storing html in databse encode them using HTML_entities(), then to display them use html_entity_decode()
当您在数据库中存储html时使用HTML_entities()对它们进行编码,然后使用html_entity_decode()显示它们
<?php
$orig = "I'll \"walk\" the <b>dog</b> now";
$a = htmlentities($orig);
$b = html_entity_decode($a);
echo $a; // I'll "walk" the <b>dog</b> now
echo $b; // I'll "walk" the <b>dog</b> now
?>
#1
2
Can't you just do some text processing instead of HTML parsing
你不能只做一些文本处理而不是HTML解析
$input = "<table><tr><td>country </td><td>US</td></tr><tr><td>state</td><td>Washington</td></tr><tr><td>city</td><td>Bingen</td></tr></table>";
$needle = array("<table>", "<tr><td>","</td<td>", "</td</tr>", "</table");
$replacements = array("","",":-","\n","");
$output = str_replace($needle, $replacements, $input);
This will give you the same as the text-input.
这将为您提供与文本输入相同的功能。
#2
2
When you are storing html in databse encode them using HTML_entities(), then to display them use html_entity_decode()
当您在数据库中存储html时使用HTML_entities()对它们进行编码,然后使用html_entity_decode()显示它们
<?php
$orig = "I'll \"walk\" the <b>dog</b> now";
$a = htmlentities($orig);
$b = html_entity_decode($a);
echo $a; // I'll "walk" the <b>dog</b> now
echo $b; // I'll "walk" the <b>dog</b> now
?>