php - php - 用用户输入的 2 个或更多子字符串替换用户字符串的确切部分 - 怎么办?
问题描述
我正在构建一个 web 应用程序,用户键入特殊类型的代码(见下文),我的这个脚本“解码”并创建代码的各个变体,然后将每个代码发送到 SQL 查询。现在,我被困在代码需要转换它的部分:
A1 B2 - C3 + D4 / E5 / E6 C3 / A5
对于这些(将斜杠“/”解释为 OR):
A1 B2 - C3 + D4 C3
A1 B2 - C3 + E5 C3
A1 B2 - C3 + E6 C3
A1 B2 - C3 + D4 A5
A1 B2 - C3 + E5 A5
A1 B2 - C3 + E6 A5
这是我现在得到的:
<?php
// User input
$input = "A1 B2 - C3 + D4 / E5 / E6 C3 / A5";
print_r($input);
// Check how many times user input have "/" (returns INT in $occurencesOfOR and makes complex array $matches of matched substrings)
$occurencesOfOR = preg_match_all("/(?: \/ )?(?:[A-IK-Za-ik-z]|Aa)[1-9][0-9]{0,2}[A-Za-z]? \/ (?:[A-IK-Za-ik-z]|Aa)[1-9][0-9]{0,2}[A-Za-z]?(?: \/ (?:[A-IK-Za-ik-z]|Aa)[1-9][0-9]{0,2}[A-Za-z]?)*/", $input, $matches);
print_r($occurencesOfOR);
// Replacing places of matched pattern with X
$dummyCode = "X";
$inputStripped = preg_replace("/(?: \/ )?(?:[A-IK-Za-ik-z]|Aa)[1-9][0-9]{0,2}[A-Za-z]? \/ (?:[A-IK-Za-ik-z]|Aa)[1-9][0-9]{0,2}[A-Za-z]?(?: \/ (?:[A-IK-Za-ik-z]|Aa)[1-9][0-9]{0,2}[A-Za-z]?)*/", $dummyCode, $input);
print_r($inputStripped);
print_r($matches);
// Creating simple array with individual OR codes
$orCodesArray = [];
foreach ($matches[0] as $match) {
array_push($orCodesArray, $match);
}
print_r($orCodesArray);
// Creating multidimensional array from individual codes created by preg_split()
$individualCodesSubarrays = [];
foreach ($orCodesArray as $code) {
$codeSet = preg_split("/ \/ /", $code);
array_push($individualCodesSubarrays, $codeSet);
}
print_r($individualCodesSubarrays);
// Count how many subarrays has $individualCodesArray (returns INT)
$counterOfArrays = count($individualCodesSubarrays);
echo $counterOfArrays;
// Getting position of X-es in stripped user input
$lastPos = 0;
$positions = [];
$position = "";
while (($lastPos = strpos($inputStripped, $dummyCode, $lastPos)) !== false) {
$positions[] = $lastPos;
$lastPos = $lastPos + strlen($dummyCode);
}
foreach ($positions as $position) {
echo $position."\n";
}
但从那里我不知道如何进一步追求。我找不到解决方案如何制作一个函数,该函数将第一个替换X
为每个,D4 / E5 / E6
第二个替换X
为每个,C3 / A5
以进行上述所有可能的组合。任何帮助表示赞赏。
更新
我成功地做到了这一点,但仍然有一个问题让它发挥作用。
// Addition to the upper code at the end
$inputStripped2 = [];
for ($c=0; $c < $counterOfArrays; $c++) {
$individualCodesSubrray = $individualCodesSubarrays[$c];
print_r($individualCodesSubrray);
$countCodes = count($individualCodesSubrray);
echo $countCodes."\n";
for ($x=0; $x < $countCodes; $x++) {
$dummyCodeX = "X".$c;
array_push($inputStripped2, str_replace($dummyCodeX, $individualCodesSubrray[$x], $inputStripped));
}
print_r($inputStripped2);
}
解决方案
您可以实现所需的一种方法是首先将字符串拆分为空格(单独)、“+”或“-”,例如
$input = "A1 B2 - C3 + D4 / E5 / E6 C3 / A5";
$parts = preg_split('/(?<=[A-Za-z0-9])\s+(?=[A-IK-Za-ik-z])|\s+([+-])\s+/', $input, -1, PREG_SPLIT_DELIM_CAPTURE);
这给出了:
Array (
[0] => A1
[1] => B2
[2] => -
[3] => C3
[4] => +
[5] => D4 / E5 / E6
[6] => C3 / A5
)
然后可以/
使用以下方法进一步拆分这些值:
foreach ($parts as &$part) {
$part = preg_split('#\s+/\s+#', $part);
}
这给出了以下数组数组:
Array (
[0] => Array (
[0] => A1
)
[1] => Array (
[0] => B2
)
[2] => Array (
[0] => -
)
[3] => Array (
[0] => C3
)
[4] => Array (
[0] => +
)
[5] => Array (
[0] => D4
[1] => E5
[2] => E6
)
[6] => Array (
[0] => C3
[1] => A5
)
)
您现在可以使用array_reduce
以下函数获取这些数组的叉积:
function cross_product($array, $value) {
$output = array();
foreach ($array as $arr) {
foreach ($value as $val) {
$output[] = array_merge($arr, array($val));
}
}
return $output;
}
$init = array_map(function ($v) { return array($v); }, array_shift($parts));
$outputs = array_reduce($parts, 'cross_product', array($init));
最后,您可以输出每个$outputs
数组的内爆版本以给出您想要的结果:
foreach ($outputs as $output) {
echo implode(' ', $output) . "\n";
}
输出:
A1 B2 - C3 + D4 C3
A1 B2 - C3 + D4 A5
A1 B2 - C3 + E5 C3
A1 B2 - C3 + E5 A5
A1 B2 - C3 + E6 C3
A1 B2 - C3 + E6 A5
或者,对于输入,"A1 / B2 - C3 + D4 / E5 / E6 C3 + A5"
您将获得:
A1 - C3 + D4 C3 + A5
A1 - C3 + E5 C3 + A5
A1 - C3 + E6 C3 + A5
B2 - C3 + D4 C3 + A5
B2 - C3 + E5 C3 + A5
B2 - C3 + E6 C3 + A5
推荐阅读
- xml - 选择性 xml 属性的 XSLT 映射
- graylog - 无法在灰色日志中搜索
- python - 不支持“未知”类型的输入。支持的类型:[azureml.data.tabular_dataset.TabularDataset, azureml.pipeline.core
- javascript - 使用 Selenium 在 Chrome 中拖放 SVG 元素的解决方法?
- wpf - 具有大型集合的 WPF ListView 挂起 GUI
- android - 使用 Android Emulator M1 Mac 测试,我该如何解决这个 ADB 问题?
- git - 清理后:git commit -m 使用单引号 - windows git-cmd.exe 和 vim 上的错误路径规范
- c# - AppSettings.config - 将字符串拆分到列表中
- regex - JMeter如何使用正则表达式提取器从响应中的项目数组中提取ID
- c# - 将传递给参数的日期时间转换为字符串c#