Asked  7 Months ago    Answers:  5   Viewed   34 times

I am trying to write a function to clean up user input.

I am not trying to make it perfect. I would rather have a few names and acronyms in lowercase than a full paragraph in uppercase.

I think the function should use regular expressions but I'm pretty bad with those and I need some help.

If the following expressions are followed by a letter, I want to make that letter uppercase.

 ". " (followed by a space)
 "! " (followed by a space)
 "? " (followed by a space)

Even better, the function could add a space after ".", "!" and "?" if those are followed by a letter.

How this can be achieved?


$output = preg_replace('/([.!?])s*(w)/e', "strtoupper('\1 \2')", ucfirst(strtolower($input)));

Since the modifier e is deprecated in PHP 5.5.0:

$output = preg_replace_callback('/([.!?])s*(w)/', function ($matches) {
    return strtoupper($matches[1] . ' ' . $matches[2]);
}, ucfirst(strtolower($input)));
Wednesday, March 31, 2021
answered 7 Months ago

mysql_real_escape_string won't escape slashes. Even escapeshellarg won't do it. You will have to use str_replace:

$path = str_replace('/', '_', $path);
Saturday, May 29, 2021
answered 5 Months ago

There's a good answer here:

function toTitleCase(str) {
    return str.replace(/wS*/g, function(txt){
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();

or in ES6:

var text = "foo bar loo zoo moo";
text = text.toLowerCase()
    .split(' ')
    .map((s) => s.charAt(0).toUpperCase() + s.substring(1))
    .join(' ');
Tuesday, June 1, 2021
answered 5 Months ago

You can use the following substitution:

  • < matches the start of a word
  • . matches the first character of a word
  • u tells Vim to uppercase the following character in the substitution string (&)
  • & means substitute whatever was matched on the left-hand side
  • g means substitute all matches, not only the first
Saturday, July 31, 2021
answered 3 Months ago

I've been using HTMLPurifier for sanitizing the output of a rich text editor, and ended up with:


$config = HTMLPurifier_Config::createDefault();
$config->set('Core', 'Encoding', 'UTF-8');
$config->set('HTML', 'Doctype', 'HTML 4.01 Transitional');

if (defined('PURIFIER_CACHE')) {
    $config->set('Cache', 'SerializerPath', PURIFIER_CACHE);
} else {
    # Disable the cache entirely
    $config->set('Cache', 'DefinitionImpl', null);

# Help out the Purifier a bit, until it develops this functionality
while (($cleaner = preg_replace('!<(em|strong)>(s*)</1>!', '$2', $input)) != $input) {
    $input = $cleaner;

$filter = new HTMLPurifier($config);
$output = $filter->purify($input);

The main points of interest:

  1. Include the autoloader.
  2. Create an instance of HTMLPurifier_Config as $config.
  3. Set configuration settings as needed, with $config->set().
  4. Create an instance of HTMLPurifier, passing $config to it.
  5. Use $filter->purify() on your input.

However, it's entirely overkill for something that doesn't need to allow any HTML in the output.

Monday, August 2, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :