Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to remove characters in front-end-form (preg_replace) WordPress

  • SOLVED

Hi dear community!

i have a front-end form with a editor-field, and is working. But there is a bug, in evry empty line appears "rn"

I know there is a way to remove it, like this..

preg_replace("/\r\n|\r|\n/",'<br/>',$description) but it didnt worked..

this is my input from part:


<?php
$mytext_var=get_the_content('Read more'); // this var may contain previous data that was stored in mysql.
wp_editor($mytext_var,"description", array('textarea_rows'=>12, 'editor_class'=>'mytext_class'));
?>


and to save it a have this in array...

'post_content' => esc_sql($_POST['description']),

thanks in advance for any help!

Answers (2)

2016-01-31

Rempty answers:

nl2br will replace all \r\n for break space(<br/>)

$mytext_var=get_the_content('Read more'); // this var may contain previous data that was stored in mysql.
$mytext_var=nl2br($mytext_var);
wp_editor($mytext_var,"description", array('textarea_rows'=>12, 'editor_class'=>'mytext_class'));


Patrick comments:

i tried it like this.. no success..

<?php
/*
Template Name: Korrekturlesen
*/
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty($_POST['post_id']) && ! empty($_POST['post_title']) && isset($_POST['update_post_nonce']) && isset($_POST['postcontent']) )
{
$post_id = $_POST['post_id'];
$post_type = get_post_type($post_id);
$capability = ( 'page' == $post_type ) ? 'edit_page' : 'edit_post';
if ( current_user_can(edit_private_posts, $post_id) && wp_verify_nonce( $_POST['update_post_nonce'], 'update_post_'. $post_id ) && ($betaval == $usval ) )
{
$description= nl2br($description);

$post = array(

'ID' => esc_sql($post_id),
'post_content' => esc_sql($_POST['description']),
'post_title' => esc_sql($_POST['post_title']),
'post_status' => 'korrekturlesen'
);
wp_update_post($post);


Patrick comments:

to change the text-field like this

<div class="ex3col";><?php the_content('Read more...'); ?></div>
<div style="margin-top: -70px; width: 370px; margin-left: -10px;"><img src="http://web216.webgo24-server47.de/undefined/wp-content/uploads/2015/06/grad-large.png" alt="grad3" width="480" height="120" class="alignnone size-full wp-image-13482" /></div>
<?php


was allso wrong : /


Rempty comments:

<?php echo nl2br(get_the_content('Read more...')); ?>


Patrick comments:

this looks like to remove the rn from the content before displaying.. i need it in my form.. but this was allso not working in the get the content part


Rempty comments:

where you set the content to your form?
this must be like this:

$mytext_var=nl2br($mytext_var);

wp_editor($mytext_var,"description", array('textarea_rows'=>12, 'editor_class'=>'mytext_class'));

Don't forget delete your content and save again.


Patrick comments:

in this part i get the editor, fill it with the content (mytext_var) and set "description" - its a meta field.

$mytext_var=get_the_content('Read more'); // this var may contain previous data that was stored in mysql.
wp_editor($mytext_var,"description", array('textarea_rows'=>12, 'editor_class'=>'mytext_class'));


then i update the post like this..the meta field "description" is going to post_content. I think somewhere there it thas to be replaced.

if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty($_POST['post_id']) && ! empty($_POST['post_title']) && isset($_POST['update_post_nonce']) && isset($_POST['postcontent']) )
{
$post_id = $_POST['post_id'];
$post_type = get_post_type($post_id);
$capability = ( 'page' == $post_type ) ? 'edit_page' : 'edit_post';
if ( current_user_can(edit_private_posts, $post_id) && wp_verify_nonce( $_POST['update_post_nonce'], 'update_post_'. $post_id ) && ($betaval == $usval ) )
{

$post = array(

'ID' => esc_sql($post_id),
'post_content' => esc_sql($_POST['description']),
'post_title' => esc_sql($_POST['post_title']),
'post_status' => 'korrekturlesen'
);
wp_update_post($post);

if ( isset($_POST['description']) ) update_post_meta($post_id, 'description', esc_sql($_POST['description']) );


i think it could be replaced like this..


$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);


Rempty comments:

You must do before show the content in the wp_editor no before save
Because, when you will edit the post, you load again the "content" and will create a bucle adding infinte /r/n each time you save the post.


Patrick comments:

i tried it.. no success : /

2016-01-31

dimadin answers:

Are you looking for something like http://php.net/manual/en/function.nl2br.php ?


Patrick comments:

yes, it looks like what i need... should i use a global function to remove it?


dimadin comments:

Sorry, I am not understanding you here, what do you mean by "global function" and to remove what?


Patrick comments:

<?php
function nl2br2($string) {
$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);
return $string;
}
?>

something like this..


Patrick comments:

i think its not possible to remove it, before saving.. but if i save it, my meta field value will be set up as 'post_content' => esc_sql($_POST['description']), -


dimadin comments:

Why don't you try


$string = nl2br( $string );


This can be before saving or before displaying, you don't need str_replace().


Patrick comments:

nl2br is a kind of php shortcode? i expect in my case it would be rn2br?


dimadin comments:

It is a regular PHP function. Which means that you use it as is. Best thing would be to try it and see if it works for you.