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? 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 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 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); 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); } Link to comment https://forums.phpfreaks.com/topic/285375-replacing-tags/#findComment-1465288 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.