想要处理一组字符串,并在每个字符串的末尾修剪一些结尾的"myEnding" (如果它存在).
最简单的方法是什么?我知道regexp一切皆有可能,但因此似乎是一个简单的任务,我想知道是否存在一个更简单的工具.
谢谢
吉迪
dqh*_*cks 11
ima在这个上使用preg_replace.
$output = preg_replace('/myEnding$/s', '', $input);
Run Code Online (Sandbox Code Playgroud)
试试这个:
$s = "foobarmyEnding";
$toRemove = "myEnding";
$len = strlen($toRemove);
if (strcmp(substr($s, -$len, $len), $toRemove) === 0)
{
$s = substr($s, 0, -$len);
}
Run Code Online (Sandbox Code Playgroud)