Php: remove all tags, but “a href” in a text

后端 未结 2 578
無奈伤痛
無奈伤痛 2021-01-26 04:52

Here\'s my problem:

I have a textarea where the user can enter whatever he wants.

When he sends this text (POST method), on the server side I don\'t filter it

2条回答
  •  伪装坚强ぢ
    2021-01-26 05:43

    Just add a preg_replace() function to revert the escaped a tags after your htmlentities() function

    $output = textForWeb($output);
    $output = preg_replace('#<a href="(?=https:\/\/|http:\/\/)(.*?)">(.*?)</a>#i', '$2', $output);
    
    echo $output;
    

    That way you can still escape all other HTML in a safe way (instead of using strip_tags() function.)

    This preg_replace() function searches for a tags linking to pages starting with http:// or https:// and then replaces the escaped special characters with <, > and ", making the link clickable again.

提交回复
热议问题