strip_tags

  • This function’s solution will only work within the context of the Rails framework.

    Rails has a series of helpers to help sanitize output. These are all in ActionView’s ActionView::Helpers::SanitizeHelper module. We can strip tags from text in Rails using the sanitize method which takes a list of optional tags or attributes as a whitelist.

    PHP

    $content = "<strong>Test</strong> some <em>tags<em>";
    $result = strip_tags($content, '<em>');
    var_export($result);
    // => 'Test some <em>tags<em>'

    Rails

    # in app/views/posts/index.html.erb
    <% @text = "<strong>Test</strong> some <em>tags<em>" %>
    <%= sanitize(@text, :tags => %w(em), :attributes => %w(style)) %>
    # => "Test some <em>tags<em>"

    Rails also has helpers to sanitize_css, strip_tags, strip_links

    see also

Looking for Rails or PHP web application development, integration, and training?

Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.