Pick first letter of each word

Asked

Viewed 439 times

1

I am using the following code to capture the first letter of each word. How can I add one more rule in the preg_split to ignore words such as: da, do, das, dos, de?

$nome = preg_split("/[\s,_-]+/", $loja->nome);
$iniciais = "";
foreach($nome as $n)
   $iniciais .= $n[0];

echo $iniciais;

1 answer

1


<?php

$nome = preg_split("/((de|da|do|dos|das)?)[\s,_-]+/", "A C Loja do Supermercado");
$iniciais = "";
foreach($nome as $n)
{
    if (strlen($n) > 0)
    {
       $iniciais .= $n[0];
    }
}

echo $iniciais;

Check the example ONLINE Ideone

Reference: preg_split

  • It didn’t work. I don’t think this is the problem, since the code without removing these words is working, but the code is inside a foreach, because I need to get the initials of all stores in the system.

  • @Diegovieira What was the problem of not working? what happened, because, I even managed a test page and it worked! now for example I made an adjustment to be without the problem of empty fields check! and let me know and if you have problems report the problem!

  • 1

    Now it worked perfectly! Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.