从PHP中的字符串中删除单词可以通过以下步骤实现:
$str = "This is a sample string";
$words = explode(" ", $str);
$words = array_filter($words, function($word) {
$wordsToDelete = array("is", "a"); // 需要删除的单词列表
return !in_array($word, $wordsToDelete);
});
$newStr = implode(" ", $words);
完整的代码如下:
$str = "This is a sample string";
$words = explode(" ", $str);
$words = array_filter($words, function($word) {
$wordsToDelete = array("is", "a"); // 需要删除的单词列表
return !in_array($word, $wordsToDelete);
});
$newStr = implode(" ", $words);
echo $newStr;
以上代码将输出:"This sample string",其中删除了字符串中的单词"is"和"a"。
推荐的腾讯云相关产品:腾讯云函数(云原生无服务器计算服务),可以用于处理字符串删除等简单的逻辑操作。产品介绍链接地址:https://cloud.tencent.com/product/scf
领取专属 10元无门槛券
手把手带您无忧上云