删除具有特定颜色样式PHP的span标签


remove span tag with a certain color style php

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

网页在样式颜色=#的跨度标签中包含"没有能力、影响力或权力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

我正在获取整个网站内容作为输出。如果您能为我提供一个链接,我可以在其中学习写作模式,我将不胜感激。

谢谢

你可以使用这个:

<?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];

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