Skip to content

Instantly share code, notes, and snippets.

@ms-studio
Last active November 24, 2020 10:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ms-studio/8793349 to your computer and use it in GitHub Desktop.
Save ms-studio/8793349 to your computer and use it in GitHub Desktop.
Process "Lastname, Firstname" in post titles.
<?php
/**
* Processing "Last name, First name" pairs
*
* @param string $nom_prenom : The original post title ("Wilde, Oscar").
*
* @return string : The human-readable title ("Oscar Wilde").
*
* Usage in theme:
* $post_title = firstname_lastname(get_the_title());
*
*/
function firstname_lastname($post_title) {
$pos = strpos($post_title, ",");
if ($pos === false) {
// nothing to do.
} else {
$fir = explode( ",", $post_title );
unset( $fir[0] );
$end = ltrim( implode( ",", $fir ) );
$first_name = substr($post_title, 0, $pos);
$pos++;
$post_title = substr($post_title, $pos) . ' ' . $first_name;
}
return $post_title;
}
@ms-studio
Copy link
Author

An updated version of this method: https://gist.github.com/ms-studio/3928828

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment