首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将php字符串中除英文字母、数字和键盘符号以外的所有项目替换为空

将php字符串中除英文字母、数字和键盘符号以外的所有项目替换为空
EN

Stack Overflow用户
提问于 2019-03-31 02:04:20
回答 1查看 33关注 0票数 0

我有一个php字符串,其中可能包含中文和其他不是utf8的符号。我需要删除所有这些,只需要维护英文字母,数字和常见的符号和空白。

我尝试过这样的正则表达式

代码语言:javascript
复制
    $text = preg_replace('/[^A-Za-z0-9\-]/', ' ', $words);

但是我没有得到想要的输出。我的字符串将如下所示:

代码语言:javascript
复制
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

我想移除�猫聦聙之类的东西。

EN

回答 1

Stack Overflow用户

发布于 2019-03-31 02:13:02

看起来您只想保留可打印的ASCII字符,而想去掉其余的字符。

您可以使用此正则表达式,它将保留所有可打印的ascii字符,并删除其余字符。

代码语言:javascript
复制
[^ -~]+

这个正则表达式基本上使用从空格到最后一个ascii字符的范围字符集,即代字号。

如果这对你有效,请告诉我。如果你想进一步从ASCII列表中排除更多的字符,这很容易做到。告诉我就行了。

PHP代码演示

代码语言:javascript
复制
$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);

仅打印您想要的字符。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55434268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档