首页 > 解决方案 > How to Hiding Seconds Word Chars [PHP]

问题描述

If string has more than one word

Example String: AAAAAA BBBBB CCCCCCC

Output: AAAAAA BB*** CCCC***


If string has one word do nothing

Example String: AAAAAA

Output: AAAAAA


I need to hide every last 3 chars of words if string has more than two word. I tried str_replace but I can't make it. Thanks for help

标签: phpstring

解决方案


我相信,这对你有用。

<?php

$input = "AAAA B";

$words = explode(" ", $input);
for($i = 1; $i < count($words); $i++) {
    $words[$i] = substr($words[$i], 0, -3) . "***";
}
$output = implode(" ", $words);
echo $output;
?>

但请记住,如果您的单词少于 3 个字母,它们将被替换为***.


推荐阅读