如何在simplehtmldom中的字符串中的每个单词周围添加引号


How to add quotes around each word in a string from simplehtmldom

我正试图从网页上按类获取特定标签的内容,转向纯文本,并用双引号将每个单词括起来。

include_once('simple_html_dom.php');
$html = file_get_html('http://linktosite.com');
foreach ( $html->find('.table tr td.span8') as $element ) {
    echo $element->plaintext . '<br>';
}

这会将每个表格->tr->td.span8中的文本输出到新行中。

我想从每个例子中提取文本,并将每个单词用引号括起来,并用逗号分隔,如下所示:

来自:

用引号包裹每个单词,并用逗号分隔

至:

"wrap"、"each"、"word"、"with"、"quotes"、"and"、"separate"、"within"、"comma"

我还想更进一步,只包括我在输出中指定的特定单词。我将从一个大约有200个条目的页面中获取文本,我希望$element->plaintext只显示我设置的变量中的单词。

如有任何帮助,我们将不胜感激。

感谢

include_once('simple_html_dom.php');
$html = file_get_html('http://linktosite.com');
foreach ( $html->find('.table tr td.span8') as $element )
{
    echo ' "' .$element->plaintext. '", <br /> ';
}

已编辑:忘记添加逗号。

试试这个:

$res ='';
foreach ( $html->find('.table tr td.span8') as $element ) {
  $res .= '"'.str_replace(" ", '", "',$element->plaintext ).'", ';
}
echo substr($res,0,-2);