【问题标题】:I want to have all results for content spinning - Resolved我想要内容旋转的所有结果 - 已解决
【发布时间】:2021-02-21 06:00:30
【问题描述】:

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);

【问题讨论】:

  • 显示所有可能的结果?请向我们展示当前结果与预期结果。
  • 您好,感谢您的宝贵时间。我用 2 个屏幕截图更新了我的帖子(我已经用 Word 制作了想要的结果)
  • @Miamdeschips 不要忘记也对帮助你的答案投票 :) 这样你就可以帮助社区更强大地支持你! :) 谢谢。

标签: php function display


【解决方案1】:

可能的替代方案:

<?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!
)

【讨论】:

    【解决方案2】:

    给你:

    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}!。然后它生成收集到的选项的所有可能组合,然后通过将每个组合替换到模板字符串中来生成所有结果。

    【讨论】:

    • 它正在工作!非常感谢,你很有帮助!
    • 欢迎@Miamdeschips :) 不要忘记将此答案标记为解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    相关资源
    最近更新 更多