php - 用php字符串中的任何内容替换除英文字母,数字和键盘符号以外的所有项目
问题描述
我有一个 php 字符串,其中可能包含中文和其他不是 utf8 的符号。我需要删除所有这些,并且只需要维护英文字母、数字和常用符号以及空格。
我试过这样的正则表达式
$text = preg_replace('/[^A-Za-z0-9\-]/', ' ', $words);
但我没有得到想要的输出。我的字符串将是这样的:
l 2014 ;59:316 �猫聦聙 Efficacy and Safety of Terbinafine Hydrochloride 1% Cream vs. Sertaconazole Nitrate 2% Cream in Tinea Corporis and Tinea Cruris: A Comparative Therapeutic Trial .Indian Journal of Dermatology 2013 ;58(6):457 - 460 �猫聦聙 Congenital cutis laxa with rectal and uterovaginal prolapse. Indian J Dermatol Venereol Leprol 2011 ;77:321 - 4 �猫聦聙 Dermatitis neglecta. Indian J Dermatol Venereol Leprol 2011 ;77:62 - 3 �猫聦聙 Ichthyosis bullosa of Siemens sans history of blistering: an interesting case report Journal of Pakistan Association of Dermatologists 2009 ; 19: 171 - 174 Paper presentations �猫聦聙 Study of Autologous Serum Skin Test In Chronic Idiopathic Urtic aria at Cuticon conference , Dec 2010 �猫聦聙 Scenerio of Leprosy at Tertiary Care Level Centre In Rural Central India at Cuticon conference , Nov 2009 Poster presentations �猫聦聙 Extrav
我想删除�猫聦聙
喜欢的东西。
解决方案
看起来您只想保留可打印的 ASCII 字符并希望摆脱其余字符。
您可以使用此正则表达式,它将保留所有可打印的 ascii 字符并删除其余字符,
[^ -~]+
这个正则表达式基本上使用范围字符集,从空格开始到最后一个 ascii 字符,即波浪号。
让我知道这是否适合您。如果您还想进一步从 ASCII 列表中排除一些字符,这很容易做到。让我知道。
PHP代码演示,
$words = "l 2014 ;59:316 �猫聦聙 Efficacy and Safety of Terbinafine Hydrochloride 1% Cream vs. Sertaconazole Nitrate 2% Cream in Tinea Corporis and Tinea Cruris: A Comparative Therapeutic Trial .Indian Journal of Dermatology 2013 ;58(6):457 - 460 �猫聦聙 Congenital cutis laxa with rectal and uterovaginal prolapse. Indian J Dermatol Venereol Leprol 2011 ;77:321 - 4 �猫聦聙 Dermatitis neglecta. Indian J Dermatol Venereol Leprol 2011 ;77:62 - 3 �猫聦聙 Ichthyosis bullosa of Siemens sans history of blistering: an interesting case report Journal of Pakistan Association of Dermatologists 2009 ; 19: 171 - 174 Paper presentations �猫聦聙 Study of Autologous Serum Skin Test In Chronic Idiopathic Urtic aria at Cuticon conference , Dec 2010 �猫聦聙 Scenerio of Leprosy at Tertiary Care Level Centre In Rural Central India at Cuticon conference , Nov 2009 Poster presentations �猫聦聙 Extrav";
echo preg_replace('/[^ -~]+/', '', $words);
仅打印您想要的字符。
l 2014 ;59:316 Efficacy and Safety of Terbinafine Hydrochloride 1% Cream vs. Sertaconazole Nitrate 2% Cream in Tinea Corporis and Tinea Cruris: A Comparative Therapeutic Trial .Indian Journal of Dermatology 2013 ;58(6):457 - 460 Congenital cutis laxa with rectal and uterovaginal prolapse. Indian J Dermatol Venereol Leprol 2011 ;77:321 - 4 Dermatitis neglecta. Indian J Dermatol Venereol Leprol 2011 ;77:62 - 3 Ichthyosis bullosa of Siemens sans history of blistering: an interesting case report Journal of Pakistan Association of Dermatologists 2009 ; 19: 171 - 174 Paper presentations Study of Autologous Serum Skin Test In Chronic Idiopathic Urtic aria at Cuticon conference , Dec 2010 Scenerio of Leprosy at Tertiary Care Level Centre In Rural Central India at Cuticon conference , Nov 2009 Poster presentations Extrav
推荐阅读
- sql - 在没有 raw_sql 的 DJANGO 中将三个表相乘
- python - 我正在构建一个 python 脚本来用一个 .csv 文件填充一个 sql 数据库,并且我不断收到一个 sql 语法错误
- list - 如何将函数应用于列表中元组中的特定元素
- mapbox - Mapbox 导航仅使用我的 2 个快速导航点
- docker-compose - 在docker-compose中,为什么连接到桥A的容器可以ping桥B?
- android - Android ARCore acquireDepthImage() 比手机屏幕小
- raspberry-pi4 - Rpi 服务随机失败
- python - 使用多个分隔符解析 txt 文件
- linq - 创建排序表达式
> 具有多个字段 - python-3.x - 根据给定日期提取时间间隔的开始和结束