我想要内容旋转的所有结果 - 已解决
I want to have all results for content spinning - Resolved
The outcome i want
The outcome I have
我的脚本卡住了。
我希望此脚本在我 运行 时显示所有可能的结果。
不幸的是,我找不到可以让我在浏览器中显示所有结果的功能。
当我启动它时,它显示一个结果,每次刷新都会改变。
有人有曲目吗?
<?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} to you, {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) );
我的结果是这样的:
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!
)
The outcome i want The outcome I have
我的脚本卡住了。
我希望此脚本在我 运行 时显示所有可能的结果。 不幸的是,我找不到可以让我在浏览器中显示所有结果的功能。 当我启动它时,它显示一个结果,每次刷新都会改变。
有人有曲目吗?
<?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} to you, {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) );
我的结果是这样的:
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!
)