Jump to content

preg_replace of a BLOCKQUOTE


Xeoncross

Recommended Posts

Hello everyone!  ;D

 

Today I am having fun trying to get another element in or around a blockquote so I can have two CSS elements to work with. (like having a start and end quote image behind my quotes). So I want to add a "DIV" either before or after my blockquote - I am just having some weird happenings...

 

I thought that str_replace would work but it gives really weird results with the added div in another area of the text. Can anyone help me with this?

 

<?php
//This is a small version of the text:
$text = '....</p><blockquote><p>This is the text</p><p>This is the text</p></blockquote><p>....';

First I tried this:
$text = str_replace('<blockquote>', '<div class="blockquote"><blockqoute>', $text);
$text = str_replace('</blockquote>', '</blockquote></div>', $text);

print $text;
?>

 

That gave me the following:

 

<div class="blockquote"><blockqoute></blockqoute><p>...</p><p>... And ...</p></div>

 

So then I went for some preg_replace and here is the code:

 

<?php
//This is a small version of the text:
$text = '....</p><blockquote><p>This is the text</p><p>This is the text</p></blockquote><p>....';

$text = preg_replace('/<blockquote>^(.*?)$<\/blockquote>/', '<div class="blockquote"><blockquote>\\1</blockquote></div>', $text);

print $text;
?>

 

Which gave me this:

<blockquote><p>...</p><p>...and ...</p></blockquote>

 

As you can see my regex code isn't working and the str_replace is just nuts.  8)

So I need some help - Thanks!

Link to comment
https://forums.phpfreaks.com/topic/59479-preg_replace-of-a-blockquote/
Share on other sites

I don't know if this will work for you in this case, but do you know about the :before and :after pseudo-elements?  (http://www.w3.org/TR/CSS21/selector.html#before-and-after)

 

Incidentally, your code works for me:

 

<?php
$text = '....</p><blockquote><p>This is the text</p><p>This is the text</p></blockquote><p>....';
echo htmlentities($text),"\n";

$text = str_replace('<blockquote>', '<div class="blockquote"><blockquote>', $text);
$text = str_replace('</blockquote>', '</blockquote></div>', $text);

echo htmlentities($text),"\n";
?>

OUTPUT:
....</p><blockquote><p>This is the text</p><p>This is the text</p></blockquote><p>....
....</p><div class="blockquote"><blockquote><p>This is the text</p><p>This is the text</p></blockquote></div><p>....

Well, I found out that the str_replace WAS working - just Firefox was saying that the code was incorrect (see below) and was closing the blockquote for me! What the filp?!

 

...</p><p><div class="blockquote"><blockqoute></p><p>...</p><p></blockquote></div></p><p>...

(View page)

 

...</p><p><div class="blockquote"><blockqoute></blockquote></p><p>...</p><p></div></p><p>...

(View selection)

 

I can't believe that Firefox is messing with a page's code and then showing you the what IT says should be on a page!!!!

Agh!!!!!

 

SO anyways, now I need to delete the <p> and </p> that is around the blockquote so that it will be "valid".

I am trying this now to fix the problem:

 

<?php
$text = preg_replace('/(<p>)?<(\\?)blockquote>(<(\\?)p>)?/', '<$2blockquote>', $text);
?>

But it doesn't work  ;D

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.