用PHP替换dom中的文本字符串

时间:2020-09-30 16:09:43

标签: php dom

<label for="options_958_1" class="custom-option-label multi 
options_958_1_label  height-cell- 27" data-column="column-height" data- 
column-parent="column-parent-height"><span class="default- 
title">6200°</span>:<span data-option-id="958" class="custom-option- 
sku">27</span></label>`

我正在尝试从dom中获取6200°的值,然后将其替换为新文本。

例如,我想用8500代替6200。

这是我在进行一些搜索后一直使用的代码,但是对于php来说还是很新的并且无法获取该值。

$ kelvin2700 = find('span [class =“。options_958_1_label”]');

1 个答案:

答案 0 :(得分:0)

如果我对您的理解正确,那么类似的事情应该可以使您朝正确的方向前进:

$doc = new DOMDocument();
$doc->loadHTML('<?xml version="1.0" encoding="UTF-8"?><label for="options_958_1" class="custom-option-label multi options_958_1_label  height-cell- 27" data-column="column-height" data-column-parent="column-parent-height">   <span class="default-title">6200°</span>   <span data-option-id="958" class="custom-option-  sku">27</span></label>');
$xpath = new DOMXpath($doc);
$temp = $xpath->query('//label/span[@class="default-title"]');
echo $temp[0]->textContent;

输出:

6200°
相关问题