如何将格式化的textarea文本保存到MySQL并再次加载?

时间:2018-07-19 18:36:10

标签: php html mysql

我有一个页面,用户可以在其中输入文本区域。当按下按钮时,文本区域的内容将发布到PHP,然后保存在MySQL表中。但是,当我再次加载它时,所有格式都已删除,例如换行符。例如,假设将其键入到textarea中:

using System.Windows;
using System.Windows.Input;

public partial class YourWindow : Window
{
    MouseCapturePreview MouseCaptureHandler;
    //(...)

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        MouseCaptureHandler = new MouseCapturePreview(this);
        //(...)
    }

    public class MouseCapturePreview
    {
        public MouseCapturePreview(Window element)
        {
            InputManager.Current.PreProcessInput += (s, e) => {
                if (e.StagingItem.Input is MouseButtonEventArgs)
                    Handler(s, (MouseButtonEventArgs)e.StagingItem.Input);
            };

            void Handler(object sender, MouseButtonEventArgs e)
            {
                Console.WriteLine("Event Received");
                if (e.LeftButton == MouseButtonState.Pressed) {
                    Console.WriteLine("Mouse Released");
                    element.ReleaseMouseCapture();
                    Mouse.Capture(element);
                    if (!element.RestoreBounds.Contains(element.PointToScreen(e.GetPosition(element))))
                    {
                        Console.WriteLine("Clicked outside");
                        MessageBox.Show("You clicked outside, time to close.");
                        //Application.Current.Shutdown();
                        element.Close();
                    }
                } else {
                    Console.WriteLine("Mouse Captured");
                    element.CaptureMouse();
                }
            }
        }
    }

    private void Window_Activated(object sender, EventArgs e)
    {
        Console.WriteLine("Activated");
        this.CaptureMouse();
    }
}

当它被装回时,将显示如下:

Bob

43

Colorado, USA

我不希望用户在文本区域中键入任何HTML,但我确实希望保存换行格式,有什么办法可以做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以使用nl2br或使用<pre></pre>标签。

nl2br示例:

echo nl2br("Bob\r\n43\n\rColorado, USA");