Feedback Request on Title Capitalization Function Converted From JavaScript to PHP I have a function in JavaScript that converts a string value to title case using APA style:

function titleCase(str, options) {
  const stopwords = 'a an and at but by for in nor of on or so the to up yet'
  const defaults = stopwords.split(' ')

  const opts = options || {}

  if (!str) return ''

  const stop = opts.stopwords || defaults
  const keep = opts.keepSpaces
  const splitter = /(\s+|[-‑–—])/

  return str
    .split(splitter)
    .map((word, index, all) =&gt; {
      if (word.match(/\s+/)) return keep ? word : ' '
      if (word.match(splitter)) return word

      if (
        index !== 0 &amp;&amp;
        index !== all.length - 1 &amp;&amp;
        stop.includes(word.toLowerCase())
      ) {
        return word.toLowerCase()
      }

      return capitalize(word)
    })
    .join('')
}

function capitalize(str) {
  return str.charAt(0).toUpperCase() + str.slice(1)
}


Credits of above JS functions

I converted the above to PHP:

function APATitle($title, $keepSpaces = false, $stopWords = array())
{
    if (!is_string($title)) {
        return false;
    }

    if (!is_array($stopWords) || count($stopWords) &lt; 1) {
        $stopWords = array('a', 'an', 'and', 'at', 'but', 'by', 'for', 'in', 'nor', 'of', 'on', 'or', 'so', 'the', 'to', 'up', 'yet'); //Must all be lowercase!
    }

    $regexWordSplit = '/(\s+|[-‑–—])/';
    $words = preg_split($regexWordSplit, $title, -1, PREG_SPLIT_DELIM_CAPTURE);

    foreach ($words as $i =&gt; $currWord) {
        if (preg_match('/\s+/', $currWord) === 1) {
            if (!$keepSpaces) {
                $words[$i] = ' ';
            }
            continue;
        }

        if (preg_match($regexWordSplit, $currWord) === 1) {
            continue;
        }

        $currWordLower = strtolower($currWord);
        if ($i != 0 &amp;&amp; $i != count($words) - 1 &amp;&amp; in_array($currWordLower, $stopWords)) {
            $words[$i] = $currWordLower;
            continue;
        }

        if (strlen($currWord) &gt; 0) {
            $currWord[0] = strtoupper($currWord[0]);
            $words[$i] = $currWord;
        }
    }

    return implode('', $words);
}


Everything works well (as far as I can tell). I'm looking for feedback on the above conversion and also general feedback on the PHP code (which it's maybe too much of a JS translation).

Some el cheapo testing for the PHP code:

$tests = array(
    array('in' =&gt; '', 'out' =&gt; '', 'keepSpaces' =&gt; false),
    array('in' =&gt; 'this is a test', 'out' =&gt; 'This Is a Test', 'keepSpaces' =&gt; false),
    array('in' =&gt; 'why sunless tanning is A hot trend', 'out' =&gt; 'Why Sunless Tanning Is a Hot Trend', 'keepSpaces' =&gt; false),
    array('in' =&gt; 'Satin Sheets are a Luxury you Can Afford', 'out' =&gt; 'Satin Sheets Are a Luxury You Can Afford', 'keepSpaces' =&gt; false),
    array('in' =&gt; 'the Dangers Of Hiking Without Proper Shoes', 'out' =&gt; 'The Dangers of Hiking Without Proper Shoes', 'keepSpaces' =&gt; false),
    array('in' =&gt; 'an hour or so', 'out' =&gt; 'An Hour or So', 'keepSpaces' =&gt; false),
    array('in' =&gt; 'Of the meaning Of Of', 'out' =&gt; 'Of the Meaning of Of', 'keepSpaces' =&gt; false),
    array('in' =&gt; 'Thing With     Extra Spaces', 'out' =&gt; 'Thing With Extra Spaces', 'keepSpaces' =&gt; false),
    array('in' =&gt; 'Thing with     extra spaces', 'out' =&gt; 'Thing With     Extra Spaces', 'keepSpaces' =&gt; true),
    array('in' =&gt; 'Observations of isolated pulsars and disk-fed X-ray binaries.', 'out' =&gt; 'Observations of Isolated Pulsars and Disk-Fed X-Ray Binaries.', 'keepSpaces' =&gt; false)
);

$ok = 0;
foreach ($tests as $currTest) {
    $currRes = APATitle($currTest['in'], $currTest['keepSpaces']);
    if ($currRes === $currTest['out']) {
        ++$ok;
    }
}
echo $ok == count($tests) ? 'All tests OK' : 'Test bad';
First things first: You definitely don't need to be making multiple regex function calls while looping the elements generated by a splitting regex function.  Time for an almost complete rewrite.  preg_replace_callback() is the tool that I recommend to do all the heavy lifting. 

Normally, I like to do everything in a single regex expression, but the retaining of the multiple spaces is a simple operation that is sensibly set apart.

There WILL be other fringe cases that other reviewers may shout out (e.g. handling multibyte characters or calling preg_quote() on the incoming blacklist words to avoid any regex pattern conflicts with special characters, and let's not forget about troublesome Mr McGee) but I am going to only focus on the quality of data that you have presented.

There will be other ways to write the pattern but I have a high appreciation for the clarity of (*SKIP)(*FAIL) it basically consumes substrings that should be disqualified and then discards them.  The pipe after the skip-fail non-capturing group will match qualifying consecutive letters. The fullstring matches are passed to the custom function where the first letter is converted to uppercase, then returned.  Finally the fully modified string is returned from the preg_replace_callback().

My regex pattern uses negative lookaheads with the start and end of string anchors (^ &amp; $) to implement the rule regarding capitalizing the first and last word.  Word boundaries (\b) are a great way to isolate a whole word (or parts of a hyphenated word).

Code: (Demo)

$tests = [
    ['this is a test', 'This Is a Test', false],
    ['why sunless tanning is A hot trend', 'Why Sunless Tanning Is a Hot Trend', false],
    ['Satin Sheets are a Luxury you Can Afford', 'Satin Sheets Are a Luxury You Can Afford', false],
    ['the Dangers Of Hiking Without Proper Shoes', 'The Dangers of Hiking Without Proper Shoes', false],
    ['an hour or so', 'An Hour or So', false],
    ['Of the meaning Of Of', 'Of the Meaning of Of', false],
    ['Thing With     Extra Spaces', 'Thing With Extra Spaces', false],
    ['Thing with     extra spaces', 'Thing With     Extra Spaces', true],
    ['Observations of isolated pulsars and disk-fed X-ray binaries.', 'Observations of Isolated Pulsars and Disk-Fed X-Ray Binaries.', false]
];

function APATitle($title, $keepMultipleSpaces = false, $blackList = [])
{
    if (!is_string($title)) {
        return false;
    }

    $blackList = $blackList
        ? array_map('strtolower', (array)$blackList)
        : ['a', 'an', 'and', 'at', 'but', 'by', 'for', 'in', 'nor', 'of', 'on', 'or', 'so', 'the', 'to', 'up', 'yet'];

    if (!$keepMultipleSpaces) {
        $title = preg_replace('~\s+~', ' ', $title);
    }

    return preg_replace_callback(
        '~(?!^)\b(?:' . implode('|', $blackList) . ')\b(?!$)(*SKIP)(*FAIL)|\b[a-z]+\b~',
        function ($m) {
            return ucfirst($m[0]);
        },
        strtolower($title)
    );
}

foreach ($tests as [$input, $expectedOutput, $keepMultipleSpaces]) {
    $output = APATitle($input, $keepMultipleSpaces);
    echo ($output === $expectedOutput ? 'SUCCESS' : 'FAILURE') . ":\t\"{$input}\" became \"{$output}\"\n";
}


Output:

SUCCESS:    "this is a test" became "This Is a Test"
SUCCESS:    "why sunless tanning is A hot trend" became "Why Sunless Tanning Is a Hot Trend"
SUCCESS:    "Satin Sheets are a Luxury you Can Afford" became "Satin Sheets Are a Luxury You Can Afford"
SUCCESS:    "the Dangers Of Hiking Without Proper Shoes" became "The Dangers of Hiking Without Proper Shoes"
SUCCESS:    "an hour or so" became "An Hour or So"
SUCCESS:    "Of the meaning Of Of" became "Of the Meaning of Of"
SUCCESS:    "Thing With     Extra Spaces" became "Thing With Extra Spaces"
SUCCESS:    "Thing with     extra spaces" became "Thing With     Extra Spaces"
SUCCESS:    "Observations of isolated pulsars and disk-fed X-ray binaries." became "Observations of Isolated Pulsars and Disk-Fed X-Ray Binaries."