简体   繁体   English

从字符串的开头或结尾删除非标准字符

[英]Remove Non Standard character from begining or end of string

I am trying to manipulate some sentences, and I can remove the non standard characters from a string, but is there any way to do this only if it is at the beginning or end of a string? 我正在尝试操纵一些句子,并且可以从字符串中删除非标准字符,但是只有在字符串的开头或结尾时,有什么方法可以执行此操作吗? To remove non standard characters I am using the following: 要删除非标准字符,我使用以下方法:

 preg_replace("/[^A-Za-z0-9 ]/", '', $string);

I would like to change a string like this: 我想更改这样的字符串:

"* This is a sentence. --" “ *这是一个句子。”

To be this: 要这样:

"This is a sentence." “这是一个句子。”

This isn't 100% robust, but this works: 这不是100%健壮的,但这可行:

preg_replace("/(?:^[^A-z0-9]+|[^.A-z0-9]+$)/", "", $input_lines);

Demo 演示版

Basically, it's replacing either anything at the beginning for the line that isn't Az, 0-9 with '' (since there are no matching groups) or ( | ) at the end anything after a full stop that isn't 0-9, Az. 基本上,它将用“(因为没有匹配的组)在开头的非Az,0-9开头的任何内容替换为,或在结尾的( | )末尾替换不是0-的任何内容。 9,Az。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM