Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7883971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:40:32+00:00 2026-06-03T04:40:32+00:00

I’ve searched the PHP manual, Stackoverflow, and some forums, but I’m stumped on some

  • 0

I’ve searched the PHP manual, Stackoverflow, and some forums, but I’m stumped on some PHP logic. Maybe I’m just tired, but I’d really appreciate some assistance or direction on this by anybody.

I have a PHP string, say:

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

Eventually, I would ideally like my final output to look something like this, but just getting the array for now is more than fine:

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

There’s a catch though, sometimes the string will be slightly different:

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

Anyways, this is my first question on Stackoverflow, and thanks in advance for all the help guys!

Edit: I’m working under some certain limitations, and I can’t change any code before the string. I know in advanced all the parents (‘cats’, ‘dogs’, ‘lemurs’, ‘monkey creatures (with space)’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:40:33+00:00Added an answer on June 3, 2026 at 4:40 am

    I’ve designed an answer that will work regardless of whether or not there are spaces between “keywords”, so long as the first keyword is not plural 🙂

    Below is the code, feel free to check it out, it’s really beautiful what you can do with text 🙂

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

    Here’s the output, I’ve given it to you as an array, so you can easily construct a ul / li hierarchy 🙂

    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
            )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.