Strip out HTML and Special Characters

后端 未结 9 1293
别跟我提以往
别跟我提以往 2020-12-07 12:39

I\'d like to use any php function or whatever so that i can remove any HTML code and special characters and gives me only alpha-numeric output

$des = "He         


        
9条回答
  •  無奈伤痛
    2020-12-07 13:13

    Probably better here for a regex replace

    // Strip HTML Tags
    $clear = strip_tags($des);
    // Clean up things like &
    $clear = html_entity_decode($clear);
    // Strip out any url-encoded stuff
    $clear = urldecode($clear);
    // Replace non-AlNum characters with space
    $clear = preg_replace('/[^A-Za-z0-9]/', ' ', $clear);
    // Replace Multiple spaces with single space
    $clear = preg_replace('/ +/', ' ', $clear);
    // Trim the string of leading/trailing space
    $clear = trim($clear);
    

    Or, in one go

    $clear = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($des))))));
    

提交回复
热议问题