2

我正在从网页读取数据,但我需要帮助编写 preg_replace 函数的模式。

该网页在样式 color=#767676 的 span 标签内包含“没有能力、影响或权力”

我希望能够在没有跨度标签的情况下仅输出“没有能力、影响或权力”。有什么办法可以根据 span 标签内的样式颜色来做到这一点?因为文件中还有许多其他的 span 标签。

这是我写的代码:

$link="http://www.myWebsite.com";
$inputlink = @file_get_contents($link) or die('Could not access file: $link');
    // To output the span tag that has style=color:#767676
$outputlink = preg_replace('/(<[^>]+) style="color:#767676"/i', '$1', $inputlink);
    // To remove the span tags
$string = preg_replace("/<span[^>]+\>/i", "", $outputlink);
echo strip_tags($string);//OUTPUT : Without ability, influence, or power

我将整个网站内容作为输出。如果你能给我一个链接,我可以学习写作模式,我也非常感激。

谢谢

4

1 回答 1

1

你可以使用这个:

<?php

$link = 'http://www.myWebsite.com';
$inputlink = @file_get_contents($link) or die('Could not access file: $link');

我假设页面“ http://www.myWebsite.com”是这样的:

<span style="color:#767676">Without ability, influence, or power</span> <span>if you see this part or see last part in gray color, your regexp is wrong!</span>

现在让我们写一些正则表达式

$pattern = '/<span style="color:#767676">([^<]+)(?<!<\/span>)<\/span>/';
preg_match($pattern, $text, $matches);
echo $matches[1];

它将Without ability, influence, or power不带<span>标签输出。

于 2013-09-27T07:00:12.937 回答