简体   繁体   English

PHP特殊字符修剪

[英]php special character trim

I have a set of strings like below : 我有下面的一组字符串:

fw-sophi.watcon.-.120
d-elain.heckop.-.121
sim.boosh.-.134
bh.-.elain.heckop.-.244

How would I trim the following set of strings to return only the middle section? 如何修剪以下字符串集以仅返回中间部分? expected return: 预期收益:

sophi.watcon
elain.heckop
sim.boosh
elain.heckop

It isn't practical to trim() the strings manually as there are a lot, how would I do this Programmatically ? 手动trim()字符串实在不切实际,我将如何以编程方式执行此操作?

If there are no other patterns, this should be sufficient: 如果没有其他模式,则应该足够了:

$input = 'fw-sophi.watcon.-.120
d-elain.heckop.-.121
sim.boosh.-.134
bh.-.elain.heckop.-.244';

if (preg_match_all('~(?<key>\w+\.\w+)~', $input, $matches)) {
    print_r($matches['key']);
}

Resulting array: 结果数组:

Array
(
    [0] => sophi.watcon
    [1] => elain.heckop
    [2] => sim.boosh
    [3] => elain.heckop
)

Pattern explanation: 模式说明:

~                 #pattern start 
(                 #start group to capture matched result
    ?<key>        #give a name to group. see $matches['key'].
    \w+           #one or more alphanumeric characters
    \.            #dot character. we need to escape it with \
    \w+           #one or more alphanumeric characters
)                 #end group
~                 #pattern end
x

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

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