Jump to content

Smart Text


The Little Guy

Recommended Posts

I am doing an insert, and I would like to have a "Smart Text Formatting".

 

currently what I have doesn't work:

function edit_name($nedit){
$nedit = strtolower($nedit);
$nedit = str_replace(array("/", "&", "  "), array("", "and", " "), $nedit);
$nedit = preg_replace('/^(a|an|the)\s+(.+)/i', '$2, $1', $nedit);
$nedit = preg_replace('/(?<![a-z]\')\b[a-z]/e', "strtoupper('$0')", $nedit);
return addslashes($nedit);
}

 

What I want it to do is the following:

- uppercase a letter after a quote if it is the first letter of the word

      (e.g. 'N)

- leave a letter lowercase after a quote if it isn't the first letter of a word

      (e.g. Don't)

- place "a", "an", "the", at the end of the sentence, with a comma in front of it

      (e.g. Big One, The)

- convert the ampersand to its proper text

      (e.g. You and Me)

Link to comment
https://forums.phpfreaks.com/topic/109425-smart-text/
Share on other sites

What isn't working? I added examples:

 

<pre>
<?php
$tests = array(
	'The quick brown fox jumps over the lazy dog.',
	"'yes,' he responded; he didn't know.",
	'You & Me.'
);
function edit_name($nedit){
	$nedit = strtolower($nedit);
	$nedit = str_replace(array("/", "&", "  "), array("", "and", " "), $nedit);
	$nedit = preg_replace('/^(a|an|the)\s+(.+)/i', '$2, $1', $nedit);
	$nedit = preg_replace('/(?<![a-z]\')\b[a-z]/e', "strtoupper('$0')", $nedit);
	return addslashes($nedit);
}
foreach ($tests as $test) {
	echo edit_name($test), '<br>';
}
?>
</pre>

 

Quick Brown Fox Jumps Over The Lazy Dog., The

\'Yes,\' He Responded; He Didn\'t Know.

You And Me.

Link to comment
https://forums.phpfreaks.com/topic/109425-smart-text/#findComment-561284
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.