I want to remove all HTML codes like " € á ... from a string using REGEX.
String: "This is a string " € á &"
Output Required: This is a string
I want to remove all HTML codes like " € á ... from a string using REGEX.
String: "This is a string " € á &"
Output Required: This is a string
you can try
$str="This is a string " € á &";
$new_str = preg_replace("/&#?[a-z0-9]+;/i",'',$str);
echo $new_str;
i hope this may work
DESC:
& - starting with
# - some HTML entities use the # sign
?[a-z0-9] - followed by
;- ending with a semi-colon
i - case insensitive.
$str = preg_replace_callback('/&[^; ]+;/', function($matches){
return html_entity_decode($matches[0], ENT_QUOTES) == $matches[0] ? $matches[0] : '';
}, $str);
This will work, but won't strip € since that is not an entity in HTML 4. If you have PHP 5.4 you can use the flags ENT_QUOTES | ENT_HTML5 to have it work correctly with HTML5 entities like €.