简体   繁体   中英

Uppercase after symbol or character with PHP

well I'm having a problem when I try to uppercase the first letter after a symbol or character and actually works great but if my string have spaces between words the code ignore de uppercase and print as is, for example:

$string = "•TEXT";

$string = ucfirst(strtolower($string));

$string = preg_replace_callback('/[.!?].*?\w/', create_function('$matches', 'return strtoupper($matches[0]);'),$string);

$string = preg_replace('/^•([a-z])([a-z]+)$/e', '"•" . strtoupper("\\1") . "\2"', $string);

echo $string; //This print •Text 

The above example shows the text correctly but this does not have spaces. If have space like the next code it shows all the text in lowercase, example:

$string = "•TEXT EXAMPLE";

$string = ucfirst(strtolower($string));

$string = preg_replace_callback('/[.!?].*?\w/', create_function('$matches', 'return strtoupper($matches[0]);'),$string);

$string = preg_replace('/^•([a-z])([a-z]+)$/e', '"•" . strtoupper("\\1") . "\2"', $string);

echo $string; //This print •text example (This is my problem, all is lowercased)

Can you help me with this?

How about a simpler solution:

$str = '•TEXT EXAMPLE';
if (preg_match('/(^&[^;]+;)(.*)/', $str, $matches)) {
    list($discard, $entity, $text) = $matches;
    // list(, $entity, $text) = $matches; // this works too
    $string = $entity . trim(ucfirst(strtolower($text)));
    echo $string;
} else {
    # Match attempt failed
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM