PHP 代码:从字符串中删除特定文字
以下 PHP 代码展示了如何从字符串中删除特定文字:
$str = 'This is a sample string.';
$remove = 'sample ';
$newstr = str_replace($remove, '', $str);
echo $newstr; // 输出:'This is a string.'
代码说明:
$str:包含需要删除文字的原始字符串。$remove:需要从字符串中删除的文字。str_replace($remove, '', $str):使用str_replace函数将$remove中的文字替换为空字符串,从而从$str中删除指定的文字。$newstr:保存删除文字后的新字符串。echo $newstr:输出新的字符串。
通过这段代码,您可以轻松地从字符串中删除指定内容。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/lWVS 著作权归作者所有。请勿转载和采集!