1

Capitalize after point and space only the next word after point, maybe using regex + ucfirst..

preg_match('/[\s\.][a-z]/'
$theCode = str_replace(ucfirst.....

In summary is to do uppercase only in the first letter after spaço + point, in all data of the variable.

$theCode = 'babab. babab babab. bababa bababa bababa. bababa babab baba';

out:

$theCode = 'babab. Babab babab. Bababa bababa bababa. Bababa babab baba';

No matter what method I adopted, I just suggested.

Thanks

1 Answers1

2

For php, preg_replace_callback + ucfirst

live example

$theCode = 'babab. babab babab. bababa bababa bababa. bababa babab baba';
$pattern = '/([a-z][^.]*)/i';

$result = preg_replace_callback($pattern, function($matches) {
    return ucfirst($matches[0]);
}, $theCode);

echo $result;

For javascript,

function capitalizeAll(str) {
   return str.replace(/([a-z])([^.]*)/gi, (a, b, c) => {
      return (b || '').toUpperCase() + c;
   });
}

var theCode = 'babab. babab babab. bababa bababa bababa. bababa babab baba';

console.log(capitalizeAll(theCode));
Val
  • 21,938
  • 10
  • 68
  • 86
  • There is no need in capture group. You're using `$matches[0]` only so `/[a-z][^.]*/i` would [do perfectly well](http://sandbox.onlinephpfunctions.com/code/497ffd7244215c51768209ef772864564965f383). – Dmitry Egorov Jun 03 '17 at 03:07
  • @DmitryEgorov what's your suggestion to make php code better? =) – Val Jun 03 '17 at 03:10
  • The PHP code is fine and pretty much matches to what I was going to post but you won the race:) – Dmitry Egorov Jun 03 '17 at 03:13
  • If any non-Latin characters may be found at the beginning of a sentence, you may need to replace `[a-z]` with [`\p{L}`](http://www.regular-expressions.info/unicode.html). `ucfirst` would also fail in the case. `mb_strtoupper` would have to be used instead: [demo](http://sandbox.onlinephpfunctions.com/code/6b789685e65ae7d0f2a14fb81bb7be630bba545a) – Dmitry Egorov Jun 03 '17 at 03:15
  • @DmitryEgorov it's ok we're both just trying to be helpful. 40 minutes faster can't tell a fair race though – Val Jun 03 '17 at 03:15
  • @DmitryEgorov Thank you! I know nothing about Spainish capitalization.. – Val Jun 03 '17 at 03:18
  • 1
    In the JavaScript snippet the conditional operator may be omitted since `/([a-z][^.]*)/` always returns a match which is at least one character long. You may also have a somewhat shorter substitution function if break the sentence into the first letter and the rest: [`str.replace(/([a-z])([^.]*)/gi, (a, b, c) => { return b.toUpperCase()+c })`](https://jsfiddle.net/cg4pwvup/) – Dmitry Egorov Jun 03 '17 at 03:25
  • You're right, this one looks more elegant. can I use this, or maybe you could have another answer with Latin language capitalize? – Val Jun 03 '17 at 03:35
  • Great @Val It was PHP, sorry I was absent, and I really forgot to say the language! Thank you. It worked very well. – Ariane Martins Gomes Do Rego Jun 03 '17 at 04:18