View Full Version : Quick PHP help needed - replacing text on the fly


mr chris
09-01-2009, 13:00
I'm developing a site for a client and I've hit rather an annoying snag. We're using TinyMCE as a WYSIWYG editor, with paste as plain text forced on (you really don't want to see what the source for documents pasted without this looks like!), but pasting from Word adds annoying

<p>&nbsp;</p>

tags after each paragraph and two more at the end of the article. Currently I'm getting the client to remove each line individually, but I was wondering if any PHP gurus on here could point me in the right direction for a script that will strip out the extra lines and only the extra lines when the articles are displayed on their appropriate pages?

There's no option of changing WYSIWYG editors at the moment, so it's just the PHP I'm after.

Anyone?

John
09-01-2009, 13:47
TinyMCE allows for custom buttons to be created and have their own functionality.

But to answer your question... try (it is untested)

$result = str_replace("<p>&nbsp;</p>", "", $originalText);

You need to change the $result and $originalText to their correct names

Added:
If you want the exact line number, filename name and correct variable names for this then I have a feeling you won't get an answer unless someone here already done this or they are willing to spend some time doing the job for you.

piscosour
09-01-2009, 15:06
Are you on *nix? Could do it in one line with perl or sed, e.g. with GNU sed you could do this:

sed -i.bak '/<p>&nbsp;<\/p>/d' *.html

Backs up all files ending in .html to .html.bak (just in case :hihi:), and removes lines containing your string (note I escaped the "/" in the closing tag). The change is performed "in place".

mr chris
09-01-2009, 18:18
Yeeeeaaaaaaaaah, that's what I figured. It's just something the client will have to live with for the time being.

And piscosour, thanks for the suggestion, but I'm afraid I will always be the coder who uses bits and pieces because he knows they work, but will never actually [I]write[/I anything. Except HTML and CSS, but they're markup anyway. It's a shared server so I don't really have enough power to do anything like that. Or ssh access.

mrmist
09-01-2009, 19:18
Doesn't TinyMce offer a paste from word button?

http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/paste

John
09-01-2009, 19:33
Doesn't TinyMce offer a paste from word button?

http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/paste

Seems like it.

Mr_chris, You need to modify the Javascript where shown in blue:

I dont have tinyMCE to test this.

tinyMCE.init({
theme : "advanced",
mode : "textareas",
plugins : "paste",
theme_advanced_buttons3_add : "pastetext,pasteword,selectall",
paste_create_paragraphs : false,
paste_create_linebreaks : false,
paste_use_dialog : true,
paste_auto_cleanup_on_paste : true,
paste_convert_middot_lists : false,
paste_unindented_list_class : "unindentedList",
paste_convert_headers_to_strong : true,
paste_insert_word_content_callback : "convertWord"
});

function convertWord(type, content) {
switch (type) {
case "before":
break;

case "after":
content = content.replace(/<p>&nbsp;<p>/,""));

break;
}

return content;
}

mr chris
10-01-2009, 02:14
Unfortunately, paste from word doesn't cover Ctl+V, which is what people will invariably use (damn their eyes!). Thanks for all the suggestions, but I think I'll just let the client worry about it for now!