PHP-将dom文档的nodeValue转换为字符串或数组?

PHP-将dom文档的nodeValue转换为字符串或数组?,php,Php,我想将表格中的文本从html文件转换为字符串,并将它们添加到数组中,但是 $doc=new DOMDocument(); $doc->loadHTMLFile('table.html'); $table=$doc->getElementsByTagName('table'); $s=$table->item(0)->nodeValue; echo $s; // it's ok , i got string . $arr=explode(' ', $s); //i add

我想将表格中的文本从html文件转换为字符串,并将它们添加到数组中,但是

$doc=new DOMDocument();
$doc->loadHTMLFile('table.html');
$table=$doc->getElementsByTagName('table');
$s=$table->item(0)->nodeValue;
echo $s; // it's ok , i got string .
$arr=explode(' ', $s); //i add string to array but..
echo "<br>";
echo count($arr); //why this string when explode to array has 1917 element??
echo "<pre>";
print_r($arr);  // and it has many space element ??
echo "</pre>";
$doc=newDOMDocument();
$doc->loadHTMLFile('table.html');
$table=$doc->getElementsByTagName('table');
$s=$table->item(0)->nodeValue;
echo$s;//没关系,我有绳子。
$arr=爆炸(“”,$s)//我将字符串添加到数组中,但。。
回声“
”; 回波计数(arr)//为什么这个字符串在分解到数组时有1917个元素?? 回声“; 打印($arr);//它有很多空间元素?? 回声“;
如何删除数组中元素之间的空间?还有别的办法吗?
我想要字符串中的数组号,如下所示: $arr[0]=1.85, $arr[1]=1.84, $arr[2]=1.75, .....

这是table.html文件:

实际上,您做错了,您需要在锚标记中循环并丢弃非数字字符

Array
(
    [0] => 1.85
    [1] => 1.84
    [2] => 1.75
    [3] => 1.74
    [4] => 2.05
    [5] => 2.09
    [6] => 2.21
    [7] => 2.25
)

如果只需要第一个表中的锚定标记值,请尝试以下操作:

$doc=new DOMDocument();
$doc->loadHTMLFile('table.html');
$table=$doc->getElementsByTagName('table');
$s=$table->item(0)->nodeValue;

$arr[]=$s; //i add string to array but..
preg_match_all('/(([\+|\-]{1})?\d(.{1})?)+/', $arr[0], $matches);
echo "<pre>";
print_r( $matches[0]);
echo "</pre>";
输出:

 Array ( [0] => -0.25 [1] => 1.85 [2] => 1.84 [3] => 1.75 [4] => 1.74 [5] => 2.05 [6] => 2.09 [7] => 2.21 [8] => 2.25 ) 
选项2 如果需要字符串中的所有数值,请尝试以下操作:


您的预期输出是什么?您希望$arr中有什么?编辑问题并提供示例输入和预期输出。否则问题就不清楚了。投票结束。我想要字符串中的数组数,如下:$arr[0]=1.85,$arr[1]=1.84,$arr[2]=1.75,。。。。。
Array ( [0] => 1.85 [1] => 1.84 [2] => 1.75 [3] => 1.74 [4] => 2.05 [5] => 2.09 [6] => 2.21 [7] => 2.25 ) 
$doc=new DOMDocument();
$doc->loadHTMLFile('table.html');
$table=$doc->getElementsByTagName('table');
$s=$table->item(0)->nodeValue;

$arr[]=$s; //i add string to array but..
preg_match_all('/(([\+|\-]{1})?\d(.{1})?)+/', $arr[0], $matches);
echo "<pre>";
print_r( $matches[0]);
echo "</pre>";
 Array ( [0] => -0.25 [1] => 1.85 [2] => 1.84 [3] => 1.75 [4] => 1.74 [5] => 2.05 [6] => 2.09 [7] => 2.21 [8] => 2.25 )