PHP:检查字符串是否包含软连字符并替换它


PHP: Check if string contains soft hyphen and replace it

我需要检查字符串是否包含软连字符,以及是否包含将其从字符串中删除。关于在将字符串保存到数据库之前找到它并将其删除的最佳方法的任何建议?

软连字符在这里是不可见的,但这会删除它:

$str = str_replace('­', '', $str);

您链接的软连字符(如果您的字符串是 ISO 8859-1):

$str = strtr($str, array("'xAD" => ""));

否则,您需要在您使用的编码中找到它的字节序列,为常见字符集的软连字符 (U+00AD) 提供一些值:

 'xAD      ISO-8859-1; ISO-8859-2; ISO-8859-3; ISO-8859-4; ISO-8859-5; 
           ISO-8859-6; ISO-8859-8; ISO-8859-9; ISO-8859-15; Windows 1250; 
           Windows 1251; Windows 1252; Windows 1253; Windows 1254; Windows 
           1255; Windows 1256; Windows 1257; Windows 1258
 'xF0      OEM 775; OEM 850; OEM 852; OEM 855; OEM 857; OEM 858;
 'xC2'xAD  UTF-8

这里的答案都不适合我。我能够删除软连字符的唯一方法是:

preg_replace('~'x{00AD}~u', '', $str);

试试这个:

$str = str_replace('-','',$str);