Jump to content

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);
Edited by AbraCadaver
Link to comment
https://forums.phpfreaks.com/topic/285375-replacing-tags/#findComment-1465283
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.