PHP如何;“不重复”;复制字符串中的每个字符后的字符串(还原字符串)
您好,我需要“取消复制”字符串的帮助(也称为恢复对字符串所做的更改)。我的PHP代码中有一个函数,它复制字符串中的每个字符(“Hello”变成“hheelllo”等等)。现在我想把它恢复过来,但我不知道怎么做(也就是我想把我的“hheelloo”变成“Hello”) 代码如下:PHP如何;“不重复”;复制字符串中的每个字符后的字符串(还原字符串),php,string,duplicates,undo,revert,Php,String,Duplicates,Undo,Revert,您好,我需要“取消复制”字符串的帮助(也称为恢复对字符串所做的更改)。我的PHP代码中有一个函数,它复制字符串中的每个字符(“Hello”变成“hheelllo”等等)。现在我想把它恢复过来,但我不知道怎么做(也就是我想把我的“hheelloo”变成“Hello”) 代码如下: <?php error_reporting(-1); // Report all type of errors ini_set('display_errors', 1
<?php
error_reporting(-1); // Report all type of errors
ini_set('display_errors', 1); // Display all errors
ini_set('output_buffering', 0); // Do not buffer outputs, write directly
?>
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Untitled 1</title>
</head>
<body>
<?php
if(isset($_POST["dupe"]) && !empty($_POST["input"])){
$input = $_POST["input"];
$newstring = "";
for($i = 0; $i < strlen($input); $i++){
$newstring .= str_repeat(substr($input, $i,1), 2);
}
echo $newstring;
}
if(isset($_POST["undupe"]) && !empty($_POST["input"])){
}
?>
<form method="post">
<input type="text" name="input" placeholder="Input"></input><br><br>
<button type="submit" name="dupe">Dupe</button>
<button type="submit" name="undupe">Undupe</button>
</form>
</body>
</html>
无标题1
欺骗
解开
现在我不知道当我按下“解开”按钮时该怎么办。(顺便说一句,如果我在这篇文章中犯了任何错误,我是stackoverflow的新手。)。由于字符串顺序没有改变,只需运行字符串并跳过第二个字符:
$undupe = '';
for($i = 0; $i < strlen($duped); $i += 2) {
$undupe .= $duped[$i]
}
也可以在两个字符之间使用和。替换为空字符串
$str = preg_replace('/.\K./', "", $str);
这将删除所有其他字符。或
请注意,此正则表达式不会验证每个奇数字符是否匹配偶数。检查这将使您的代码正常工作:
$newstring = "";
for($i=0, $size=strlen($input); $i < $size; $i+=2){
$newstring .= $input[$i];
}
$newstring=”“;
对于($i=0,$size=strlen($input);$i<$size;$i+=2){
$newstring.=$input[$i];
}
另外,以防万一,.如果有人在复制字符串之前单击“Undupe”,行为应该是什么?哦!谢谢我不知道可以使用数组索引从字符串中获取单个字符。($dupped[$i])。
$newstring = "";
for($i=0, $size=strlen($input); $i < $size; $i+=2){
$newstring .= $input[$i];
}