使用php进行html内容的str_replace

时间:2013-07-11 12:52:35

标签: php html regex str-replace

我有这段代码:

$str = 'di <a href="http://www.cadoinpiedi.it/author/redazione-la/#C" style="color:#006699; text-decoration:none;">VARIABLE NAME</a>'.
                '<br>'.
                '<strong>POST TITLE</strong>'.
                '<br>'.
                '<br>';
                $content=str_replace($str, "", $content);

有了它,我会从RSS描述中删除此内容,如下所示:

注意:$ STR由变量名称和每个项目更改的帖子标题组成!

<img src="http://www.cadoinpiedi.it/img/fico-balo.JPG" width="280" height="94" align="left" style="margin-right:10px;" hspace="10" > 
<div style="margin-right:10px;" >
di <a href="http://www.cadoinpiedi.it/author/redazione-la/#C" style="color:#006699; text-decoration:none;">Redazione Cadoinpiedi.it</a>
<br>
<strong>La showgirl ha denunciato la coppia per diffamazione</strong>
<br>
<br>

Raffaella Fico ha querelato i coniugi Balotelli, che rischiano un processo per il reato di diffamazione aggravata, perché commessa a mezzo stampa. Sulla Gazzetta dello Sport del 27 dicembre scorso i Balotelli avevano scritto una lettera aperta alla modella. "Nostro figlio non è quell'essere irresponsabile e senza dignità che tu... <a href="http://www.cadoinpiedi.it/2013/07/11/raffaella_fico_porta_in_tribunale_i_genitori_di_balotelli.html" style="color:#006699; text-decoration:none;"> Leggi </a>

</div>

它不起作用......我做错了什么? 非常感谢。

3 个答案:

答案 0 :(得分:0)

你的字符串连接不正确。传递给函数时,它可能导致$str等于0。使用.进行连接。

$str = 'di <a href="http://www.cadoinpiedi.it/author/redazione-la/#C";'
     . 'style="color:#006699; text-decoration:none;">' . $VARNAME . '</a>' 
     . '<br>' 
     . '<strong>' . $POSTTITLE . '</strong>' 
     . '<br>' 
     . '<br>';

答案 1 :(得分:0)

$content = substr($content,0,182).substr($content,448);

它不会修复您的代码,这可能是您在问题评论中提到的+连接问题,但是,它应该做您想要的。

更新以反映OP问题的变化:

$startPos = stripos($content, "di <");
$endPos = strripos($content, "<br>", $startPos) + 10;
$content = substr($content, 0, $startPos) . substr($content, $endPos);

答案 2 :(得分:0)

$endPos = stripos($content, "<br>");
                echo "<br><br><br>start:".$startPos."<br><br><br>end". $endPos." <br><br><br>";
                $content = substr($content, $endPos, strlen($content)-1);
                echo "<br><br><br>ecco: ".$content;