Use str_replace substitur tag <br> for <p>

Asked

Viewed 34 times

-1

People need to replace tags br for p in my xml constructor and for this I am using the method str_replace but he’s not making that substitution.

Follows the code:

$i=1;
foreach ($xml->channel->item as $item){
   if($i <= 30){        
       if(substr($item->link, 0, 23) == 'https://www.site.com.br'){
    $imagem = '';
    if($item->images->image != ''){
        $imagem = '<figure> <img src="'.str_replace(' ','',str_replace('alt=','',str_replace('"','',str_replace('src=','',str_replace('/>','',$item->images->image))))).'"/>  </figure>';
    }
    $newItem = $newXML->addChild('item');
    $newItem->addChild('title', $item->title);
    $newItem->addChild('link', $item->link);
    $newItem->addChild('guid', $item->guid);
    $newItem->addChild('pubDate', $item->pubDate);
    $newItem->addChild('content', '
    <![CDATA[

        <head>
          <meta charset="utf-8">
          <link rel="canonical" href="'. $item->link .'">
        </head>
        <body>
          <article>
            <header>
            <h1>'.$item->title.'</h1>
            '.str_replace('<h2></h2>','','<h2>'.$item->summary.'</h2>').'
            '.$imagem.'
                            </header>

            <!-- Article body goes here -->

            '.str_replace('<br><br>','</p><p>',$item->content).'
            <figure class="op-tracker">
                <iframe src="https://www.site.com.br?nota=' . $item->link .'" hidden></iframe>
            </figure>


            <footer>

            </footer>
          </article>
        </body>
    </html>
    ]]> ');
$i +=1;
}}  
}
print($newXML->asXML('teste.xml'));

1 answer

0

Note that for you to be able to do this type of substitution, the code returned in $item->contentfor example, it needs to be exactly <br><br> without any character between them. If it is <br/>, it won’t work or if it’s just one <br> neither.

Share a snippet of your xml code (if possible). Then we will arrive at a more appropriate solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.