我的问题是我无法捕获此标记内的文本:
<p class="name">
" Eau de Toillete for Men, Spray 110ml " </p>
如您所见,文本用引号引起来
” 厕所的Eua 男士喷雾,110ml“
这是我的代码:
$pos1 = ".h2marca";
$pos2 = "[id=landing-submarca-perfume] h1";
$pos3 = "[class=name]";
$pos4 =".price";
$contador = 0
while (!empty($titulo3 = trim($html2->find($pos3,$contador)->plaintext)))
{
$titulo1 = trim($html2->find($pos1,0)->plaintext);
$titulo2 = trim($html2->find($pos2,0)->plaintext);
$titulo3 = trim($html2->find($pos3,$contador)->plaintext);
$titulo3 = str_replace("for Women, ","",$titulo3);
$titulo3 = str_replace("for Men, ","",$titulo3);
$titulo= $titulo1 . " " . $titulo2 . " " . str_replace("."," ",$titulo3);
$precio = trim($html2->find($pos4,$contador)->innertext);
$contador++;
}
我需要使用“$contador”,因为此网络中还有其他添加,需要捕获所有内容。
$título3 捕获一个空白区域。
我需要捕获文本而不删除 $contador 变量
你能帮我吗?这是示例网页 http://www.fundgrube.es/es/perfumes/aramis/aramis.html
谢谢!
最佳答案
有点绕房子,但这可能有用:
$split_this = '<p class="name">
" Eau de Toillete for Men, Spray 110ml " </p>';
$split_this = strip_tags($split_this, '');
$split_this = str_replace('"','',$split_this);
$split_this = trim($split_this);
$split_this = '"' . $split_this . '"';
给<p id="ptag1">
标记一个 id 并放置一个隐藏的输入
<input type="hidden" name="ptag_value" />
使用 JavaScript 你可以设置
document.getElementById('ptag_value').value = document.getElementById('ptag1').innerHTML;
如果他们的服务器支持fopen
$handle = fopen("http://www.fundgrube.es/es/perfumes/aramis/aramis.html", "r");
$contents = stream_get_contents($handle);
$explode( '<p class="name">', $contents ); // may not work
echo $contents[0]; // 1, 2, 3 , 4, etc
或
strip_tags($contents, '<p>'); // should preserve the p tags
否则使用空白''
strip_tags($contents, ''); // not entirely predictable but can work
应该只保留所有文本,不包含任何 html。其他示例:
https://stackoverflow.com/questions/15281124/php-split-explode-stringstrong text
关于PHP 简单 HTML DOM - 获取稀有标签内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33966704/