<?php
/**
* 在文章的第N个句号后插入指定字符串
*
* @param string $article 要处理的文章内容
* @param int $n 要插入的位置(第N个句号后)
* @param string $insertString 要插入的字符串
* @Return string 处理后的文章内容,若N超出范围则返回原文章
*/
function insertAfterNthPeriod($article, $n, $insertString) {
// 查找所有句号的位置
$periodPositions = [];
$lastPos = 0;
while (($lastPos = strpos($article, '。', $lastPos))!== false) {
$periodPositions[] = $lastPos;
$lastPos++;
}
// 确保第N个句号存在
if (isset($periodPositions[$n - 1])) {
// 计算插入位置(第N个句号后)
$insertPosition = $periodPositions[$n - 1] + 1;
// 插入字符串
return substr($article, 0, $insertPosition) . $insertString . substr($article, $insertPosition);
}
// N超出范围,返回原文章
return $article;
}
// 示例用法
$testCases = [
[
'article' => "这是第一段内容。这是第二段内容。这是第三段内容。",
'n' => 2,
'insertString' => "插入的新内容。",
'expected' => "这是第一段内容。这是第二段内容。插入的新内容。这是第三段内容。"
],
[
'article' => "测试。数据。案例。",
'n' => 1,
'insertString' => "插入测试。",
'expected' => "测试。插入测试。数据。案例。"
],
[
'article' => "只有一个句号。",
'n' => 1,
'insertString' => "插入成功。",
'expected' => "只有一个句号。插入成功。"
],
[
'article' => "无句号文本",
'n' => 1,
'insertString' => "插入失败",
'expected' => "无句号文本"
],
[
'article' => "第一段。第二段。第三段。第四段。",
'n' => 4,
'insertString' => "结尾插入。",
'expected' => "第一段。第二段。第三段。第四段。结尾插入。"
]
];
// 执行测试
foreach ($testCases as $index => $testCase) {
$result = insertAfterNthPeriod($testCase['article'], $testCase['n'], $testCase['insertString']);
echo "测试用例 #" . ($index + 1) . ":\n";
echo "原文: " . $testCase['article'] . "\n";
echo "操作: 在第" . $testCase['n'] . "个句号后插入 '" . $testCase['insertString'] . "'\n";
echo "预期: " . $testCase['expected'] . "\n";
echo "结果: " . $result . "\n";
if ($result === $testCase['expected']) {
echo "✅ 测试通过\n";
} else {
echo "❌ 测试失败\n";
}
echo "------------------------\n";
}
?>
/**
* 在文章的第N个句号后插入指定字符串
*
* @param string $article 要处理的文章内容
* @param int $n 要插入的位置(第N个句号后)
* @param string $insertString 要插入的字符串
* @Return string 处理后的文章内容,若N超出范围则返回原文章
*/
function insertAfterNthPeriod($article, $n, $insertString) {
// 查找所有句号的位置
$periodPositions = [];
$lastPos = 0;
while (($lastPos = strpos($article, '。', $lastPos))!== false) {
$periodPositions[] = $lastPos;
$lastPos++;
}
// 确保第N个句号存在
if (isset($periodPositions[$n - 1])) {
// 计算插入位置(第N个句号后)
$insertPosition = $periodPositions[$n - 1] + 1;
// 插入字符串
return substr($article, 0, $insertPosition) . $insertString . substr($article, $insertPosition);
}
// N超出范围,返回原文章
return $article;
}
// 示例用法
$testCases = [
[
'article' => "这是第一段内容。这是第二段内容。这是第三段内容。",
'n' => 2,
'insertString' => "插入的新内容。",
'expected' => "这是第一段内容。这是第二段内容。插入的新内容。这是第三段内容。"
],
[
'article' => "测试。数据。案例。",
'n' => 1,
'insertString' => "插入测试。",
'expected' => "测试。插入测试。数据。案例。"
],
[
'article' => "只有一个句号。",
'n' => 1,
'insertString' => "插入成功。",
'expected' => "只有一个句号。插入成功。"
],
[
'article' => "无句号文本",
'n' => 1,
'insertString' => "插入失败",
'expected' => "无句号文本"
],
[
'article' => "第一段。第二段。第三段。第四段。",
'n' => 4,
'insertString' => "结尾插入。",
'expected' => "第一段。第二段。第三段。第四段。结尾插入。"
]
];
// 执行测试
foreach ($testCases as $index => $testCase) {
$result = insertAfterNthPeriod($testCase['article'], $testCase['n'], $testCase['insertString']);
echo "测试用例 #" . ($index + 1) . ":\n";
echo "原文: " . $testCase['article'] . "\n";
echo "操作: 在第" . $testCase['n'] . "个句号后插入 '" . $testCase['insertString'] . "'\n";
echo "预期: " . $testCase['expected'] . "\n";
echo "结果: " . $result . "\n";
if ($result === $testCase['expected']) {
echo "✅ 测试通过\n";
} else {
echo "❌ 测试失败\n";
}
echo "------------------------\n";
}
?>