<?php
$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";
}
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."
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
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."