Jump to content

Replacing <p> tags


skippt

Recommended Posts

Hi,

I'm trying to replace <p> tags and replace it with a contenteditable div.

$newpost = str_replace("<p>", "<div contenteditable=\"true\"><p>", $post);

This code works fine however I'm using javascript setInterval to periodically update to a database through ajax and while it doesn't conflict with javascript when I refresh the page it inserts a second string replace.

eg.
On first refresh it enters

<div contenteditable="true"><p>

On second refresh it enters

<div contenteditable="true"><div contenteditable="true"><p>

On third refresh it enters

<div contenteditable="true"><div contenteditable="true"><div contenteditable="true"><p>

I've tried replacing the <p> with <P> or <p class="paragraph"> and str_replace instead of str_ireplace but I still get the same result.

Does anyone know of any way I can only have it replace once?

Link to comment
https://forums.phpfreaks.com/topic/285375-replacing-tags/
Share on other sites

Though not the best solution, this should work:

$newpost = str_replace("<div contenteditable=\"true\"><p>", "<p>", $post);
$newpost = str_replace("<p>", "<div contenteditable=\"true\"><p>", $newpost);

EDIT: Actually, your other idea should work:

$newpost = str_replace("<p>", "<div contenteditable=\"true\"><p class=\"paragraph\">", $post);
Link to comment
https://forums.phpfreaks.com/topic/285375-replacing-tags/#findComment-1465283
Share on other sites

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.