2015-11-28 52 views
2

我的问题是,我不能抓住这个标签中的文字:PHP简单的HTML DOM - 获取文本罕见的标签内

<p class="name"> 
    "      Eau de Toillete for Men, Spray 110ml  "  </p> 

正如你所看到的,文字是用引号

“ 男性香水 的EUA,110毫升喷“

这是我的代码:

$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捕获一个空的空间。

我需要捕获文本而不删除$康塔多变量

你能帮助我吗?这是示例web http://www.fundgrube.es/es/perfumes/aramis/aramis.html

谢谢!

回答

2

有点一轮的房屋,但是这可能工作:

$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

+0

谢谢!你帮了我很多! * 我使用此代码: $ titulo3 = strip_tags(trim($ html2-> find($ pos3,$ contador) - > plaintext)); $ titulo3 = str_replace函数( '“', ”“,$ titulo3); 回声$ titulo3”
“; 立足您的第一个代码 – Thane

+0

很高兴它的工作 - 我能够从一个站点导入所有评论到。在理论上你应该在导入的html上运行一个正则表达式或者某种清理工具来防止注入,但这可能是过度的,很高兴它可以工作 – Steve

+1

不是那样的在引号内部的空格? - trim(str_replace(''','“,$ titulo3));'应该清除它。 – Steve

1

它正常工作对我来说:

require_once('simple_html_dom.php'); 

$html = <<<EOF 
<p class="name"> 
    "      Eau de Toillete for Men, Spray 110ml  "  </p> 
EOF; 

$dom = str_get_html($html); 

echo $dom->find('p.name', 0)->plaintext; 
#=>  "      Eau de Toillete for Men, Spray 110ml  "  
相关问题