示例字符串:
AAAAAA BBBBB CCCCCCC
输出:
AAAXXX BBXXX CCCCXXX
我需要隐藏每个最后3个单词的字符。我试过str_replace但我无法做到。谢谢你的帮助
$replacement = "***";
if (stripos($name, ' ') !== false) {
$star = substr($name, 0, -3).$replacement;
}
我尝试了这个,但代码只隐藏了最后一个单词的3个字符。我需要每一个字。感谢
答案 0 :(得分:2)
您需要拆分字符串,替换最后三个字符,然后重新组合它。
$replacement = "***";
// break your string into an array based on the spaces
$temp = explode(' ', $name) ;
// our temporary name
$newName = '' ;
// loop through each part of the original name
foreach($temp as $section) {
// append our modified string along with a space
$newName .= substr($section, 0, -3).$replacement . ' ' ;
}
// set $name = $newName without the trailing space
$name = substr($newName,0,-1) ;
答案 1 :(得分:0)
$replacement = "***";
$star = "";
$pieces = explode(" ", $name);
foreach ($pieces as $piece){
$star .= substr($piece, 0, -3).$replacement." ";
}
答案 2 :(得分:0)
尝试此功能,
connexion
如果您有一个句子,那么您可以使用空格对其进行切片并调用此函数,最后重新创建该句子。