0

I have a PHP variable $MostRecentQuestionType whose value is SimplifyingFractions. I want to print Simplifying Fractions, so I tried the following, but it returns Array ( [0] => Simplifyin [1] => Fractions )

$MostRecentQuestionType = $data['MostRecentQuestionType'];
$MostRecentQuestionTypeExploded = preg_split('/.(?=[A-Z])/',$MostRecentQuestionType);
print_r($MostRecentQuestionTypeExploded);

How do I extract the words from this array?

Side note: I know people are going to attack me for not doing my research; I've tried but I don't quite have the vocab to look up the solution. I've tried converting array to string and similar searches, but the results don't address this question. My searching has led to me to preg_replace, str_replace, toString(), among others, but nothing seems to help. I feel like I'm getting hung up on something that has a very easy solution.

Snoops
  • 215
  • 1
  • 12
  • Look at the other post. https://stackoverflow.com/questions/4519739/split-camelcase-word-into-words-with-php-preg-match-regular-expression – user3720435 Sep 19 '17 at 22:54

2 Answers2

2

Something like this:

<?php
$MostRecentQuestionType = "SimplifyingFractions";
$MostRecentQuestionTypeExploded = preg_split('/(?=[A-Z])/',$MostRecentQuestionType);
print(implode($MostRecentQuestionTypeExploded," "));

You could also cut out the middle man by doing:

<?php
$MostRecentQuestionType = "SimplifyingFractions";
$MostRecentQuestionTypeExploded = preg_replace('/(?=[A-Z])/'," ",$MostRecentQuestionType);
print($MostRecentQuestionTypeExploded);
  • Ah, I KNEW it had to be something simple like this. Thanks so much! I'll accept the answer when the site allows me to do so in a few minutes. – Snoops Sep 19 '17 at 22:57
0

Instead of your print_r command, you can do this :

 foreach($MostRecentQuestionTypeExploded as $value)     { print($value." ");}
Pierre Capo
  • 1,033
  • 9
  • 23
  • This doesn't even run; based on his code you're attempting to iterate a string with a foreach loop?? – Greg Rozmarynowycz Sep 19 '17 at 23:07
  • $MostRecentQuestionType at this point of the script is an array, since the preg_split() command was called just before. – Pierre Capo Sep 19 '17 at 23:10
  • `preg_split` is not an in place operation, `$MostRecentQuestionType` is a string. And even if it was, he wanted an output of a string, not dumping the array values. His issue was how the string was being split. – Greg Rozmarynowycz Sep 19 '17 at 23:15
  • Alright the stackoverflow app bugged as always and didn't take into account my edit. Forgot the Exploded, hence the confusion. – Pierre Capo Sep 19 '17 at 23:23