mejpark Posted June 5, 2009 Share Posted June 5, 2009 Good evening folks, I have a webapp that allows users to catalogue their DVD titles, and I would like to reformat all titles that start with the word "the". Specifically, if the 'title' string starts with the word "the", I want to chop "the" from the beginning of the string, prepend it with a comma and a space, and then glue it onto the end of the string: "The Matrix" (current) "Matrix, The" (desired) What is the easiest way to do this in PHP? Regards, Mike Link to comment https://forums.phpfreaks.com/topic/161124-solved-php-string-conversion-the-matrix-to-matrix-the/ Share on other sites More sharing options...
Alex Posted June 5, 2009 Share Posted June 5, 2009 function changeTitle($title) { $parts = explode(' ', $title); return $parts[1] . ', ' . $parts[0]; } $title = 'The Matrix'; $new_title = (strtolower(substr($title, 0, 3)) == 'the') ? changeTitle($title) : $title; echo $new_title; Output: Matrix, The However, that won't work for titles that contain more than 3 words and the word 'the'. How would you want want the title to change if it was like 'The something something'? I can change the function for you if necessary. Link to comment https://forums.phpfreaks.com/topic/161124-solved-php-string-conversion-the-matrix-to-matrix-the/#findComment-850242 Share on other sites More sharing options...
PFMaBiSmAd Posted June 5, 2009 Share Posted June 5, 2009 Explode has an optional 3rd parameter that when set to 1 would handle exploding at just the first space. Link to comment https://forums.phpfreaks.com/topic/161124-solved-php-string-conversion-the-matrix-to-matrix-the/#findComment-850243 Share on other sites More sharing options...
Alex Posted June 5, 2009 Share Posted June 5, 2009 Explode has an optional 3rd parameter that when set to 1 would handle exploding at just the first space. Ahh, I never knew that Use this then: function changeTitle($title) { $parts = explode(' ', $title, 2); return $parts[1] . ', ' . $parts[0]; } $title = 'The Matrix'; $new_title = (strtolower(substr($title, 0, 3)) == 'the') ? changeTitle($title) : $title; echo $new_title; If the title was 'The Matrix' it would output Matrix, The . If it was 'The Matrix Reloaded' it would output Matrix Reloaded, The. Edit: The third parameter in explode() doesn't work like that.. If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string . So it had to actually be 2. Link to comment https://forums.phpfreaks.com/topic/161124-solved-php-string-conversion-the-matrix-to-matrix-the/#findComment-850245 Share on other sites More sharing options...
mejpark Posted June 5, 2009 Author Share Posted June 5, 2009 Thanks AlexWD, that works like a charm. Interesting logic you've used there. Very useful function. Cheers, Mike Link to comment https://forums.phpfreaks.com/topic/161124-solved-php-string-conversion-the-matrix-to-matrix-the/#findComment-850277 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.