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)'
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
)
)
You'll probably want to use the preg_match_all
function and use a regular expression. That way, you don't have to use any loops:
$matches = array();
$string = 'cats cat1 cat2 cat3 dogs dog1 dog2 monkey creatures monkey_creature1 monkey_creature2 monkey_creature3'
preg_match_all('/((?:[a-z]+ )*?[a-z]+s) ((?:[a-z_]+[0-9] ?)+)*/i', $string, $matches);
// $matches now contains multidemensional array with 3 elements, indices
// 1 and 2 contain the animal name and list of those animals, respectively
$animals = array_combine($matches[1], $matches[2]);
$animals = array_map(function($value) {
return explode(' ', trim($value));
}, $animals);
print_r($animals);
Output:
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
)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With