Do you know how to replace html tags with a space character using php?
If I display
strip_tags(\'Foo
bar\');
Try this.
preg_replace('#<[^>]+>#', ' ', '<h1>Foo</h1>bar');
With the Regex solution preg_replace('/<[^>]*>/', ' ', $str)
, it's not gonna work if you have event attributes like this:
<button onclick="document.getElementById('alert').innerHTML='<strong>MESSAGE</strong>';">
click</button>
You need to do one more replacement:
<?php
$str =
"<div data-contents=\"<p>Hello!</p>\">Hi.</div>".
"Please<button onclick=\"document.getElementById('alert').innerHTML='".
"<strong>MESSAGE</strong>';\">click</button>here.";
$event =
"onafterprint|onbeforeprint|onbeforeunload|onerror|onhaschange|onload|onmessage|".
"onoffline|ononline|onpagehide|onpageshow|onpopstate|onresize|onstorage|onunload|".
"onblur|onchange|oncontextmenu|onfocus|oninput|oninvalid|onreset|onselect|onsubmit|".
"onkeydown|onkeypress|onkeyup|onclick|ondblclick|ondrag|ondragend|ondragenter|".
"ondragleave|ondragover|ondragstart|ondrop|onmousedown|onmouseenter|onmousemove|".
"onmouseleave|onmouseout|onmouseover|onmouseup|onscroll|onabort|oncanplay|".
"oncanplaythrough|oncuechange|ondurationchange|onemptied|onended|onerror|".
"onloadeddata|onloadedmetadata|onloadstart|onpause|onplay|onplaying|onprogress|".
"onratechange|onseeked|onseeking|onstalled|onsuspend|ontimeupdate|onvolumechange|".
"onwaiting|data-[^=]+";
$str = preg_replace("/<([^>]+)(".$event.")=(\"|')(?:(?!\\3).)+\\3/", "<$1", $str);
$str = preg_replace("/<[^>]*>/", " ", $str);
echo $str;
// with only strip_tags:
// Hi.Pleaseclickhere.
// with event and data attributes removal + regex tags removal:
// Hi. Please click here.
// with only regex tags removal:
// Hello! ">Hi. Please MESSAGE ';">click here.
?>
Hope it helps!
You can try
$str = '<h1>Foo</h1>bar';
var_dump(replaceTag($str,array("h1"=>"div")));
Output
string '<div>Foo</div>bar' (length=17)
Function Used
function replaceTag($str,$tags) {
foreach ( $tags as $old => $new )
$str = preg_replace("~<(/)?$old>~", "<\\1$new>", $str);
return $str;
}
preg_replace('#\<(.+?)\>#', ' ', $text);
Bit late with answer but try this one, basically selects everything inside <> including the tags.
Something like this will work if you know that >
won't be in any of your attributes.
preg_replace('/<[^>]+>/', ' ', 'hello<br>world');
I helped my self with example from user40521, but I made a function with same api like php's strip_tags, it does not use multiple variables, and it also makes a trim, so a single whitespace is removed from start / end.
/**
* @param string $string
* @param string|null $allowable_tags
* @return string
*/
function strip_tags_with_whitespace($string, $allowable_tags = null)
{
$string = str_replace('<', ' <', $string);
$string = strip_tags($string, $allowable_tags);
$string = str_replace(' ', ' ', $string);
$string = trim($string);
return $string;
}