如何在不使用任何功能的情况下用星号替换字符串的前3个字符? [英] How to replace first and last 3 characters of a string with asterisk without using any functions?
问题描述
我想用*
符号替换前3个字符和后3个字符,而无需使用任何内置函数.
I want to replace the first 3 characters and last 3 characters with *
sign without using any built in functions.
$string = array("johndoee","shawnmarsh","peterparker","johndoee","shawnmarsh","peterparker");
你能指导我吗?有什么办法吗?
Can you guide me? Is there any way to do this?
推荐答案
This seems fairly pointless, but it's possible using string access and modification by character.
foreach ($strings as &$string) {
for ($i=0; $i < 3; $i++) {
$string[$i] = '*';
$string[-($i+1)] = '*';
}
}
请注意,如果字符串包含多字节字符,此不会正常工作,因为它以字节数组的形式访问字符串.
Note that this won't work properly if the string contains multibyte characters, because it's accessing the string as a byte array.
还请注意,这需要PHP 7.1才能使用负索引.如果您没有PHP 7.1,我将不知道一种无需使用任何功能即可替换最后三个字符的方法.
Also note that this requires PHP 7.1 in order to use the negative indexes. If you don't have PHP 7.1 I don't know of a way to replace the last three characters without using any functions.
这篇关于如何在不使用任何功能的情况下用星号替换字符串的前3个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!