skippt Posted January 14, 2014 Share Posted January 14, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/285375-replacing-tags/ Share on other sites More sharing options...
AbraCadaver Posted January 14, 2014 Share Posted January 14, 2014 (edited) 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 January 14, 2014 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/285375-replacing-tags/#findComment-1465283 Share on other sites More sharing options...
sKunKbad Posted January 14, 2014 Share Posted January 14, 2014 How about: $newpost = str_replace("<p>", "<div contenteditable=\"true\"><p data-contenteditable >", $post); Quote Link to comment https://forums.phpfreaks.com/topic/285375-replacing-tags/#findComment-1465284 Share on other sites More sharing options...
JIXO Posted January 14, 2014 Share Posted January 14, 2014 Why u r not using if condition : if(preg_match('/<div contenteditable="true"><p>/', $post)) { $newpost = $post; } else { $newpost = str_replace("<p>", "<div contenteditable=\"true\"><p>", $post); } Quote Link to comment https://forums.phpfreaks.com/topic/285375-replacing-tags/#findComment-1465288 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.