删除具有某种颜色风格的span标签php

问题描述:

我正在阅读网页中的数据,但我需要帮助为preg_replace函数编写模式。删除具有某种颜色风格的span标签php

该网页包含“没有能力,影响或力量” span标签的内部风格颜色=#767676

我希望能够只输出“没有能力,影响或权力”,不跨标签。有什么办法可以根据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 

我得到了整个网站的内容输出。我也非常感谢你能否提供一个我可以学习写作模式的链接。

感谢

+0

您的意思是网页数据是否为'&span>#767676>没有能力,影响力或者力量'和**没什么其他? –

+0

不,这是一个包含许多其他span和div标签的网页。但我想不出任何其他方式来提取这个特定的文本:“没有能力,影响力或力量”,所以我在想,是否有一种方法可以根据它的颜色来提取它。 – Laura

+0

您使用过'strip_tags()',它删除了span和其他标签,那么使用'preg_replace()'的原因是什么? –

您可以使用此:

<?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>标签。

+0

我将模式更改为更短的形式。现在你可以从模式中删除'(?)'来缩短。 –