Programmatically edited Post Text displays correctly but has blank lines in the database

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
EVO_VV
Registered User
Posts: 55
Joined: Tue Feb 13, 2018 3:11 pm

Programmatically edited Post Text displays correctly but has blank lines in the database

Post by EVO_VV »

The extension I am working on requires the ability to programmatically remove certain URLs from a Post Text.
The code to search and find the URLs works perfectly, the issue I have is with editing them out of the Post Text.
Having tried and failed with the generate_text_for_edit, generate_text_for_display and generate_text_for_storage I found that knowing the format the data was stored in the database enabled me to edit the text quite simply by using str_replace.

Code: Select all

// $posttext is read from the database before editing
$a = "http://example.com";
$toremove = '<URL url="' . $a . '"><s>[url]</s>' . $a . '<e>[/url]</e></URL> <br/>';
$posttext = str_replace($toremove, "", $posttext);
$postchecksum = md5($message_parser->$posttext)
The database is then Updated with the new $postext and $postchecksum
(Also the post_edit_time, post_edit_user, post_edit_count and post_edit_reason are updated but I think that has no bearing on the issue)

On page refresh the Post is displayed correctly without the offending URL but in the database there is a blank line where the URL once was.

I did try the generate_text_for_storage after the above code but that seemed to have no effect.
While things are working how they are intended, I'm sure that the blank line in the database should not be there and would like to fix that.

What am I missing or not yet understanding ??

EDIT
Tried using $posttext = str_replace($toremove, "Link Removed by Admin", $posttext); in the above code and found that it appears differently in the Post and database.
In the Post the 'Link Removed by Admin' is displayed as Removed by Admin followed by whatever came next in the Post with no 'New Line' after the 'Removed by Admin' but there is a space.
In the database the 'Link Removed by Admin' is displayed on it's own line with whatever came next in the Post beginning on the next line.

It would appear that there is a <br/> being added somehow in the database but so far I cannot see where it might be.
The only <br/> I can see comes at the end of the link and that should be removed by the str_replace.
EVO_VV
Registered User
Posts: 55
Joined: Tue Feb 13, 2018 3:11 pm

Re: Programmatically edited Post Text displays correctly but has blank lines in the database

Post by EVO_VV »

Found the Problem.

Firstly it turns out that it was a \n not a <br> and it was coming from the original post prior to editing.
Did not spot it as when the text is rendered for display the \n is rendered as a space.

Secondly my previous attempts to find a \n failed as it has to surrounded by "double quotes" not 'single ones'.

Following code now works as intended :-
// $post_text and $bbcode_uid are read from the database before editing
decode_message($post_text, $bbcode_uid);
$a = "http://example.com";
$togo = '[url]' . $a . '[/url] '."\n";
$post_text = str_replace($togo, "", $post_text);
$post_text = $this->parser->parse($post_text);


Both Display and Database are now correct with the offending URL completely removed
Post Reply

Return to “Extension Writers Discussion”