我正在尝试获取字符串的长度并用星号(*)替换其第一个和后三个字符,并获取字符串长度而不使用函数中的任何PHP构建。
strlen - substr - preg_replace
示例:$string = "123456789"; $new_string = "***456***";
有可能吗?
我检查了许多教程,但无法弄明白。请帮忙。谢谢!
答案 0 :(得分:1)
通用解决方案:
$string = "123456789";
$i = 0;
$limit = 3;
while (isset($string[$i])) {
if ($i < $limit) {
$string[$i] = '*';
}
$i++;
}
// you can rewrite this as loop
$string[$i - 1] = '*';
$string[$i - 2] = '*';
$string[$i - 3] = '*';
从php7.1开始,允许否定字符串索引:
$string = "123456789";
// you can rewrite it to a loop too
$string[0] = $string[1] = $string[2] = $string[-1] = $string[-2] = $string[-3] = '*';
echo $string;
没有任何功能的解决方案,但它会发出PHP Notice
,您可以使用@
来阻止它:
$string = "123456789";
$i = 0;
$limit = 3;
while (true) {
if ($string[$i] == '') {
break;
}
if ($i < $limit) {
$string[$i] = '*';
}
$i++;
}
/* Simplified version:
while ($string[$i] != '') {
if ($i < $limit) {
$string[$i] = '*';
}
$i++;
}
*/
$string[$i - 1] = '*';
$string[$i - 2] = '*';
$string[$i - 3] = '*';
echo $string;
答案 1 :(得分:0)
你可以通过循环来实现它。(没有内置函数)
$string = "123456789";
$i = 0;
$new_str = "";
while(true)
{
$val = @$string[$i];
if($val == "")
break;
if($i<3)
$new_str .= "*";
else
$new_str .= $val;
$i++;
}
for($j=0;$j<$i;$j++)
{
if($j >= ($i-3))
$new_str[$j]="*";
}
echo $new_str;
答案 2 :(得分:0)
我不知道你为什么不想使用任何PHP函数。但是,它仍然有可能。
TinyMCE
将回应此
<?php
function privacy($string)
{
//get the string length
$i = 0;
/*
while (isset($string[$i])) {
$i++;
}
*/
while ($string[$i] !== "") {
$i++;
}
//replace the first and last 3 characters of the $string with a star
//if the length is bigger than or equals to 6
if ($i >= 6) {
for ($x = 0; $x < 3; $x++) {
$string[$x] = "*";
$string[$i - $x] = "*";
}
}
return $i . "<br>" . $string;
}
echo privacy("123456789");
?>
我认为这是获取字符串长度并替换第一个和最后3个字符而不会产生混淆的最简单方法。到目前为止!
祝你好运!答案 3 :(得分:-1)
您可以像访问数组一样访问字符串。
function readURL(input) {
var file = document.getElementById("article_file").value;
console.log(file);
var originalname = file.replace(/C:\\fakepath\\/i, '');
console.log(name);
if (DST()) {
// Daylight Savings, EDT is UTC-4
var offset = "-4";
} else {
// Not Daylight Savings, EST is UTC-5
var offset = "-5";
}
var today = calcTime(offset);
var month = today.getMonth() + 1;
// appends 0 to monthes under 10
if (month < 10)
month = "0" + month.toString();
if ($('table.appendo.image-uploader tbody tr').length > 2)
$('table.appendo.image-uploader tbody tr').each(function (i, ele) {
if(i==1)
{
$('#image-preview-path').attr('src','src="/images/tp.gif"');
$('#image-path').attr('value','');
}
else if (i > 1)
{
$(ele).remove();
}
});
$('.upload-article').attr('name', 'Filedata[]');
var files = $('#article_file')[0].files;
var name = [];
var extension = ['.jpg','jpeg','.gif','.png'];
var flag = 1;
name['invalid'] = 'invalid';
for (var z = 0; z < files.length; z++){
var ext = (files[z].name.substr(-4)).toLowerCase();
if($.inArray(ext, extension) != -1){
} else {
flag = 2;
}
}
var count = files.length;
if(count <= '6') {
if(flag == 1) {
for (var x = 0; x < files.length; x++)
{
if (x == 0) {
var file = files[x];
var hello = file['name'].replace(/\s/g, '');
var file_name = hello.replace(/[^.a-z0-9_-\s]/gi, '').replace(/[_\s]_/g, '-');
name[file['size']] = file_name;
console.log(files[x]);
var reader = new FileReader();
reader.addEventListener('load', function (e) {
console.log(e)
$('#image-preview-path').attr('src', e.target.result);
$('#image-path').attr('value', "/images/uploads/" + today.getFullYear() + "/" + month + "/" + today.getDate() + "/" + name[e.total]);
});
reader.onerror = function (event) {
console.error("File could not be read! Code " + event.target.error.code);
};
reader.readAsDataURL(file);
} else {
var file = files[x];
var hello = file['name'].replace(/\s/g, '');
var file_name = hello.replace(/[^.a-z0-9_-\s]/gi, '').replace(/[_\s]_/g, '-');
name[file['size']] = file_name;
console.log(files[x]);
var reader = new FileReader();
reader.addEventListener('load', function (e) {
console.log(e)
if ($('table.appendo>tbody:eq(0)>tr').length - 1 > 0)
index = $('table.appendo>tbody:eq(0)>tr').length - 1
else
index = 'invalid';
var table = $('table.appendo tbody tr:eq(1)').clone();
table.find('img').attr('id', 'image-preview-path');
table.find('img').attr('src', e.target.result);
table.find('input').attr('id', 'image-path');
table.find('input').attr('value', "/images/uploads/" + today.getFullYear() + "/" + month + "/" + today.getDate() + "/" + name[e.total]);
$('table.appendo.image-uploader tbody').append(table);
});
reader.onerror = function (event) {
console.error("File could not be read! Code " + event.target.error.code);
};
reader.readAsDataURL(file);
}
}
} else {
alert('Not a valid file extension.');
$('#article_file').val("");
}
} else {
alert('You have attempted to queue too many files.\nYou may select six file.');
$('#article_file').val("");
}
}
但我目前还不确定如何在不使用功能构建的情况下获得最后3个