How to replace one blockquote tag with span tag php

Go To StackoverFlow.com

0

I have tag in my content. I need to replace all those tags by span tags: for eg: my contenti is wrapped like :

<blockquote><p>my content</p></blockquote> .

I need to replace this with :

<span class="highlight">content</span>

How can i achieve this in PHP. I tried some preg_replace but nothing worked. Can anyone please help.

Thanks in advance.

2012-04-04 18:03
by Sabari


0

try

preg_replace('/<blockqoute>.*?<p>(.*?)<\/p>.*?<\/blockqoute>/i', '<span class="highlight">$1</span>', $string);

2012-04-04 18:14
by Edhen
It does not work :( .. Actually i need to remove the

tags inside the

as well... only the contents should come inside spa - Sabari 2012-04-04 18:19
I just edited it to add the p tags and i test before and it worked, are u assigning it to a variable? like $replace = preg_replace('/<blockqoute><p>(.*)<\/p><\/blockqoute>/i', '<span class="highlight">$1</span>', $subject); echo $replace; - Edhen 2012-04-04 18:27
One more issue i am having is that sometimes a space comes between blockquote tag and p tag like

contennt

. How can we replace that as wel - Sabari 2012-04-04 18:40
preg_replace('/<blockqoute>.*?<p>(.*)<\/p>.*?<\/blockqoute>/i', '<span class="highlight">$1</span>', $string);Edhen 2012-04-04 18:46
I updated my answer and i also forgot to include a ? inside the pattern. But that code should work fine - Edhen 2012-04-04 19:05
Your answer works fine :) thnks : - Sabari 2012-04-05 05:26


1

Certainly not the best solution, but should be good enough and doesn't require regex:

$string = str_replace('<blockquote><p>', '<span class="highlight">', $string);
$string = str_replace('</p></blockquote>', '</span>', $string);

Alternatively:

$search = array('<blockquote><p>', '</p></blockquote>');
$replace = array('<span class="highlight">', '</span>');
$string = str_replace($search, $replace, $string);
2012-04-04 18:06
by Andreas Wong
forgot the <p> and </p> tags.. - Dan O 2012-04-04 18:07
@orzechowskid thanks for pointing that out : - Andreas Wong 2012-04-04 18:10
Ads