var okTags = /^(<\/?(b|blockquote|code|del|dd|dl|dt|em|h1|h2|h3|i|kbd|li|ol|p|pre|s|sup|sub|strong|strike|ul)>|<(br|hr)\s?\/?>)$/i;

var okLinks = /^(<a\shref="(\#\d+|(https?|ftp):\/\/[-A-Za-z0-9+&@#\/%?=~_|!:,.;\(\)]+)"(\stitle="[^"<>]+")?\s?>|<\/a>)$/i;


var okImg = /^(<img\ssrc="https?:(\/\/[-A-Za-z0-9+&@#\/%?=~_|!:,.;\(\)]+)"(\swidth="\d{1,3}")?(\sheight="\d{1,3}")?(\salt="[^"<>]*")?(\stitle="[^"<>]*")?\s?\/?>)$/i;


text = text.replace(/<[^<>]*>?/gi, function (tag) {
                    return (tag.match(okTags) || tag.match(okLinks) || tag.match(okImg)) ? tag : ""
                })

最佳答案

replace()可以替换为preg_replace()

match()将是preg_match(),这非常强大

查看手册以了解其如何与您的代码一起使用,它们没有什么不同,您也可以使用回调函数。

关于php - PHP等同于此代码是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5959888/

10-16 14:38