PHP提供了丰富的字符串处理函数,以下是常用的字符串操作方法:
正则表达式是处理复杂字符串匹配的强大工具,PHP中使用PCRE库实现正则功能:
// 匹配邮箱
preg_match('/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/', $email);
// 匹配URL
preg_match('/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/', $url);
function validateEmail($email) {
return preg_match('/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/', $email);
}
// 去除HTML标签 $text = strip_tags($html); // 只保留特定标签 $text = strip_tags($html, '