admin管理员组

文章数量:1122846

I have added a custom metabox that holds string from a textarea in database. Now if I just simply echo the meta box like this:

$post_meta = get_post_meta($pid);
$answer = $post_meta["answer"][0];
echo $answer;

the html tags would be escaped and the text would appear like this:

blah blah blah <ul><li>blah blah</li><li>blah</li></ul>

As you see the html tags get escaped and they appear as string so I use php like this:

$answer = html_entity_decode($answer);

Now the html tags work as they are expected but there remains one problem. In order to get the line breaks from the database I add another line like this:

    $answer = nl2br($answer);

which solves the problem with line breaks but add a new line break between each li tags.

Any idea how to resolve this? I need line breaks but at the same time would want to avoid adding a line break between li tags.

UPDATE: for the moment I added this line of php after nl2br and I got the result

    $answer = preg_replace("!</li>[\s\S]{1,100}?<!", "</li><", $answer);

But I don't think this is the right way to go.

I have added a custom metabox that holds string from a textarea in database. Now if I just simply echo the meta box like this:

$post_meta = get_post_meta($pid);
$answer = $post_meta["answer"][0];
echo $answer;

the html tags would be escaped and the text would appear like this:

blah blah blah <ul><li>blah blah</li><li>blah</li></ul>

As you see the html tags get escaped and they appear as string so I use php like this:

$answer = html_entity_decode($answer);

Now the html tags work as they are expected but there remains one problem. In order to get the line breaks from the database I add another line like this:

    $answer = nl2br($answer);

which solves the problem with line breaks but add a new line break between each li tags.

Any idea how to resolve this? I need line breaks but at the same time would want to avoid adding a line break between li tags.

UPDATE: for the moment I added this line of php after nl2br and I got the result

    $answer = preg_replace("!</li>[\s\S]{1,100}?<!", "</li><", $answer);

But I don't think this is the right way to go.

Share Improve this question edited Sep 26, 2018 at 12:17 agahi asked Sep 26, 2018 at 8:09 agahiagahi 1013 silver badges12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

To add line-breaks inside <textarea> (but not on front-end output), use this inside textarea:

echo str_replace('</li>',"</li>\r\n", $answer);

Why don't you use <?php esc_textarea( $answer ); ?>

本文标签: custom fieldOutputing a metabox textarea and avoid line breaks inside li tags