Php 我想有内容旋转的所有结果-解决
我被我的剧本卡住了 我希望此脚本在运行时显示所有可能的结果。 不幸的是,我找不到允许我在浏览器中显示所有结果的函数。 当我启动它时,它会显示一个结果,每次刷新时都会更改 有人有轨道吗Php 我想有内容旋转的所有结果-解决,php,function,display,Php,Function,Display,我被我的剧本卡住了 我希望此脚本在运行时显示所有可能的结果。 不幸的是,我找不到允许我在浏览器中显示所有结果的函数。 当我启动它时,它会显示一个结果,每次刷新时都会更改 有人有轨道吗 <?php class Spintax { public function process($text) { return preg_replace_callback( '/\{(((?>[^\{\}]+)|(?R))*)\}/x',
<?php
class Spintax
{
public function process($text)
{
return preg_replace_callback(
'/\{(((?>[^\{\}]+)|(?R))*)\}/x',
array($this, 'replace'),
$text
);
}
public function replace($text)
{
$text = $this->process($text[1]);
$parts = explode('|', $text);
return $parts[array_rand($parts)];
}
}
/* EXAMPLE USAGE */
$spintax = new Spintax();
$string = '{Hello|Howdy|Hola} to you, {Mr.|Mrs.|Ms.} {Smith|Williams|Davis}!';
echo $spintax->process($string);
给你:
class Spintax
{
private $counter = 0;
private $options = [];
public function process($text)
{
$template = preg_replace_callback(
'/\{(((?>[^\{\}]+)|(?R))*)\}/x',
array($this, 'replace'),
$text
);
$combinations = [[]];
foreach ($this->options as $i => $list) {
$new_combinations = [];
$key = '{' . ($i + 1) . '}';
foreach ($combinations as $combination) {
foreach ($list as $item) {
$combination[$key] = $item;
$new_combinations[] = $combination;
}
}
$combinations = $new_combinations;
}
$result = [];
foreach ($combinations as $combination) {
$result[] = strtr($template, $combination);
}
return $result;
}
public function replace($text)
{
++$this->counter;
$this->options[] = explode('|', $text[1]);
return '{' . $this->counter . '}';
}
}
$spintax = new Spintax();
$string = '{Hello|Howdy|Hola} to you, {Mr.|Mrs.|Ms.} {Smith|Williams|Davis}!';
$result = $spintax->process($string);
echo implode("\n", $result);
简要说明:
函数process()
将所有可能的替代项保存到$options
属性中,并将字符串转换为模板,如{1}对您来说,{2}{3}代码>。然后,它生成所收集选项的所有可能组合,然后通过将每个组合替换到模板字符串中来生成所有结果。可能的备选方案:
<?php
class Spintax
{
public function process($text)
{
preg_match_all('/(\{[^\}]+\})/', $text, $matches);
$sets = [];
foreach($matches[0] as $match) {
$sets[$match]['replacements'] = explode('|',
str_replace(
['{', '}'],
'',
$match
)
);
$sets[$match]['results'] = [];
}
foreach($sets as $placeholder => $entries) {
$replacements = $entries['replacements'];
foreach($replacements as $replace) {
$sets[$placeholder]['results'][] = str_replace(
$placeholder,
$replace,
$text
);
foreach($sets as $innerPlaceholder => $innerEntries) {
$innerResults = $innerEntries['results'];
foreach($innerResults as $result) {
$sets[$placeholder]['results'][] = str_replace(
$placeholder,
$replace,
$result
);
}
}
}
}
$output = array_unique($sets[array_key_last($sets)]['results']);
return array_values(
array_filter(
$output,
function($entry) {
return 0 === preg_match('/\{[^\}]+\}/', $entry);
}
)
);
}
}
/* EXAMPLE USAGE */
$spintax = new Spintax();
$string = '{Hello|Howdy|Hola} to you, {Mr.|Mrs.|Ms.} {Smith|Williams|Davis}!';
print_r( $spintax->process($string) );
显示所有可能的结果?请向我们展示当前结果与预期结果。您好,谢谢您的时间。我用2个截图更新了我的帖子(我用Word获得了想要的结果)@Miamdeschips别忘了投票支持你的答案:)这样你就可以让社区更支持你了!:)谢谢,它起作用了!非常感谢你,你帮了我大忙!欢迎@Miamdeschips:)别忘了将此答案标记为解决方案
Array
(
[0] => Hello to you, Mr. Smith!
[1] => Howdy to you, Mr. Smith!
[2] => Hola to you, Mr. Smith!
[3] => Hello to you, Mrs. Smith!
[4] => Howdy to you, Mrs. Smith!
[5] => Hola to you, Mrs. Smith!
[6] => Hello to you, Ms. Smith!
[7] => Howdy to you, Ms. Smith!
[8] => Hola to you, Ms. Smith!
[9] => Hello to you, Mr. Williams!
[10] => Howdy to you, Mr. Williams!
[11] => Hola to you, Mr. Williams!
[12] => Hello to you, Mrs. Williams!
[13] => Howdy to you, Mrs. Williams!
[14] => Hola to you, Mrs. Williams!
[15] => Hello to you, Ms. Williams!
[16] => Howdy to you, Ms. Williams!
[17] => Hola to you, Ms. Williams!
[18] => Hello to you, Mr. Davis!
[19] => Howdy to you, Mr. Davis!
[20] => Hola to you, Mr. Davis!
[21] => Hello to you, Mrs. Davis!
[22] => Howdy to you, Mrs. Davis!
[23] => Hola to you, Mrs. Davis!
[24] => Hello to you, Ms. Davis!
[25] => Howdy to you, Ms. Davis!
[26] => Hola to you, Ms. Davis!
)