Hello Friends,

i want to allow '<br>' tags in my string not '<br />'...!
i want to also disallow '<br />'.
when i use strip_tags($string, '<br>'). in this case strip_tags also allow '<br />' tags.

Please, help me...!

Dani AI

Generated

Quick clarification: PHP's tag stripper doesn't enforce exact tag spelling. Allowing a <br>-type tag tells PHP to keep that tag name, and many implementations treat the self-closing slash and attributes as part of the same tag, so <br />, <BR/>, or <br class="x"> can still be preserved. As hinted, a simple normalization often fixes immediate cases and confirmed that approach works — but there are safer, more robust options.

A lightweight, reliable fix (handles different spacing, case and attributes) is to replace any <br> variant with a plain <br> using a regex callback, which strips attributes and normalizes self-closing forms:

$html = preg_replace_callback(
    '/<br\b[^>]*>/i',
    function ($m) { return '<br>'; },
    $html
);

For full correctness on real HTML (and to avoid brittle regex hacks) use DOM parsing: load the HTML with DOMDocument, remove attributes from all br nodes, and unwrap or remove any other element tags while keeping their text. That approach is tolerant of nested structures and preserves textual content safely.

Notes and cautions:

  • Regex is OK for a simple, single-tag normalization but not a general HTML sanitizer.
  • If user input can contain scripts or attributes, do not rely on strip-only methods for security; use a vetted sanitizer such as .
  • See the PHP docs for how tag stripping behaves and for regex helpers: strip_tags manual and preg_replace manual. For DOM-based handling see DOMDocument.

These approaches let you enforce exactly one plain <br> form and remove self-closing or attribute-bearing variants while avoiding unsafe assumptions.

Recommended Answers

All 2 Replies

str_replace('<br />','<br>',$string);

Hope this help!

commented: helpful reply...! thanks Zero13 +2

great...! thanks dear. my problem solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.