php - 通过单独的某些单词拆分/解析 PHP 字符串

标签 php string parsing split explode

我搜索了 PHP 手册、Stackoverflow 和一些论坛,但我对某些 PHP 逻辑感到困惑。也许我只是累了,但我真的很感激任何人对此提供的帮助或指导。

我有一个 PHP 字符串,比如:

 $string = 'cats cat1 cat2 cat3 dogs dog1 dog2 monkey creatures monkey_creature1 monkey_creature2 monkey_creature3';

最终,理想情况下我希望我的最终输出看起来像这样,但现在只获取数组就够了:

 <h2>cats</h2>
 <ul>
     <li>cat1</li>
     <li>cat2</li>
     <li>cat3</li>
 </ul>

 <h2>dogs</h2>
 <ul>
     <li>dog1</li>
     <li>dog2</li>
 </ul>

 <h2>monkey creatures</h2>
 <ul>
     <li>monkey_creature1</li>
     <li>monkey_creature2</li>
     <li>monkey_creature3</li>
 </ul>

不过有一个问题,有时字符串会略有不同:

 $string = 'cats cat1 cat2 cat3 cat4 cat5 cats6 dogs dogs1 dogs2 monkey creatures monkey_creature1 lemurs lemur1 lemur2 lemur3';

无论如何,这是我在 Stackoverflow 上的第一个问题,在此先感谢所有帮助的人!

编辑:我在某些限制下工作,我不能更改字符串之前的任何代码。我知道高级所有的 parent ('猫','狗','狐猴','猴子生物(有空间)'

最佳答案

我设计了一个无论“关键字”之间是否有空格都有效的答案,只要第一个关键字不是复数:)

下面是代码,请随意查看,你可以用文本做的事情真的很漂亮:)

<?
$string = 'cats cat1 cat2 cat3 dogs dog1 dog2 monkey creatures monkey_creature1 monkey_creature2 monkey_creature3';

$current_prefix = '';
$potential_prefix_elements = array();

$word_mapping = array();

foreach(split(" ", $string) as $substring) {
    if(strlen($current_prefix)) {
        // Check to see if the current substring, starts with the prefix
        if(strrpos($substring, $current_prefix) === 0)
            $word_mapping[$current_prefix . 's'][] = $substring;
        else
            $current_prefix = '';
    }

    if(!strlen($current_prefix)) {
        if(preg_match("/(?P<new_prefix>.+)s$/", $substring, $matches)) {
            $potential_prefix_elements[] = $matches['new_prefix'];

            // Add an 's' to make the keys plural
            $current_prefix = join("_", $potential_prefix_elements);

            // Initialize an array for the current word mapping
            $word_mapping[$current_prefix . 's'] = array();

            // Clear the potential prefix elements
            $potential_prefix_elements = array();
        } else {
            $potential_prefix_elements[] = $substring;
        }
    }
}

print_r($word_mapping);

这是输出,我已将其作为数组提供给您,因此您可以轻松构建 ul/li 层次结构:)

Array
(
    [cats] => Array
        (
            [0] => cat1
            [1] => cat2
            [2] => cat3
        )

    [dogs] => Array
        (
            [0] => dog1
            [1] => dog2
        )

    [monkey_creatures] => Array
        (
            [0] => monkey_creature1
            [1] => monkey_creature2
            [2] => monkey_creature3
        )

)

关于php - 通过单独的某些单词拆分/解析 PHP 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10442233/

相关文章:

PHP - 编辑 zip 存档中的文件并在关闭前另存为另一个存档名称

php - mysqli 从多个表中查询

php 截断字符串如果超过限制并在末尾放置一些遗漏..类似于 ruby

objective-c - Objective C 字符串操作、查看、添加

python - 如何将这个邻接图输入列表?

java - Java/C/C++/Python/shell命令行参数解析器

php - 表单ajax在提交之前插入数据

php - 如何在 echo 中打印 PHP 行

C : trying to remove spaces from a string without using any string library functions

c++ - 如何在 Spirit X3 中正确指定锚定条件?