2012-03-07 53 views
1

是否可以通过cURL获取某个类名?cURL + PHP获取类名称,可能吗?

例如,我只想得到123这个<div class="123 photo"></div>

这可能吗?

感谢您的回复!

编辑:

如果这是什么网站

<div class="444 photo">...</div> 
<div class="123 photo">...</div> 
<div class="141 photo">...</div> 

etc... 

上看到我试图让这个类的所有号码,并把在一些阵列。

+1

'cURL'只是通过不同的协议检索内容。它不搜索或操纵该内容。我们需要更多关于您想要做什么的信息,以及您目前如何尝试完成这些信息。 – Morgon 2012-03-07 17:53:54

+1

cURL没有自己的文本处理功能。我假设你问你是否可以使用PHP来提取,而答案是肯定的,你可以使用正则表达式。 – 2012-03-07 17:55:31

+0

@Morgon,你是什么意思?我只是想从另一个网站的cURL获得类名123,我需要init()或其他的东西,我仍然是新的..谢谢 – hellomello 2012-03-07 17:59:40

回答

4

cURL只是解决方案的一半。它的工作仅仅是检索内容。一旦你有了这些内容,那么你可以做字符串或结构操作。有一些可以使用的文本功能,但似乎您正在寻找这些内容中的特定内容,因此您可能需要更强大的功能。因此,对于这个HTML内容,我建议你研究一下DOMDocument,因为它会把你的内容构造成类似于XML的层次结构,但是更加宽容HTML标记的宽松性质。

$ch = curl_init(); 
// [Snip cURL setup functions] 
$content = curl_exec($ch); 

$dom = new DOMDocument(); 
@$dom->loadHTML($content); // We use @ here to suppress a bunch of parsing errors that we shouldn't need to care about too much. 

$divs = $dom->getElementsByTagName('div'); 
foreach ($divs as $div) { 
    if (strpos($div->getAttribute('class'), 'photo') !== false) { 
     // Now we know that our current $div is a photo 
     $targetValue = explode(' ', $dom->getAttribute('class')); 

     // $targetValue will now be an array with each class definition. 
     // What is done with this information was not part of the question, 
     // so the rest is an exercise to the poster. 
    } 
} 
+0

你的网址是什么?空的?很好的例子。 – delive 2016-03-02 14:14:27

+0

@delive - 据推测这将出现在“[Snip cURL setup functions]”块中:)重点在于解析数据。 – Morgon 2016-03-03 20:20:37

+0

我知道,但其他人不知道,你需要清楚发表一个例子:) – delive 2016-03-04 07:59:43