Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从字符串末尾删除撇号s(';s)_Php_Regex - Fatal编程技术网

Php 如何从字符串末尾删除撇号s(';s)

Php 如何从字符串末尾删除撇号s(';s),php,regex,Php,Regex,我想删除字符串末尾的“'s”,但无法使其在PHP中工作 我发现这个是这样的: function endsWith($haystack, $needle) { // search forward starting from end minus needle length characters return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($h

我想删除字符串末尾的“'s”,但无法使其在PHP中工作

我发现这个是这样的:

function endsWith($haystack, $needle) {
// search forward starting from end minus needle length characters
    return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
}   
我试着这样称呼它:

endsWith($word,"'s");
不幸的是,它不适用于以s结尾的单词,例如:

$word = "mom's";
$word = "dad's";
非常感谢您的任何建议。正则表达式也是一种选择,我不挑剔

编辑:从“妈妈的”(抱歉!)中删除了额外的“

该功能应该适合您

echo rtrim($word,"'s");
演示:

尝试预更换:

preg_replace("/\'s$/", "", $word);
这将只消除以
结尾的字符串,如
妈妈的
。最后没有句号,没有别的

要全部替换,请尝试:

preg_replace("/\'s/", "", $word);

Stru_替换应该有助于降低投票率,真的吗?对我来说似乎是个公平的问题原来撇号是被编码的,所以多亏了Chris,我用了这个,问题就解决了:$word=rtrim($word,';s”);单引号在双引号内时不需要转义。C编程的习惯谢谢Chris,演示效果很好,但在我的代码中,它似乎可以正确地检测字符串末尾的,但由于某些原因,rtrim只会删除“s”。例如,“爸爸的”变成了“爸爸的”。以前见过吗?这是一个真实的引用,也许是实体
'?PHP版本的行为似乎是一致的。就是这样!谢谢你,克里斯。