Add a comma to each word

Asked

Viewed 1,074 times

4

Ex.:

$str = "texto de teste";
// RESULTADO ESPERADO
$keywords = "texto, de, teste";
  • If $descricao is a string is not easier to swap blank spaces for commas?

  • I didn’t really think about it.. I seem to be complicating the situation by removing the spaces and adding a comma.. can you tell me how to do that? I think it should be with str_replace, but I have no idea in practice..

  • 1

    That’s right, with str_replace(), see if this solves your problem, have other approach. You can check the php functions in the documentation, see str_replace, => http://php.net/manual/en/function.str-replace.php. . It is normal not to remember the order of the arguments of some functions ;)

  • Thanks, I managed to do with the help of the site 'php.net' and also Vaati. Anyway, it worked. Only I will not give your answer as correct because he made it very clear, and if in the future someone has the question, you will see the answer below!

  • I suggest, working with array('word 1','word 2','word 3') then I’d just be $string_virgulas = implode(',', $dados_array).

  • This solution would be nothing practical Ivan, better to use string manipulation functions instead of creating arrays, etc.

Show 1 more comment

3 answers

6


str_replace(" ", ", ", $descricao);

Whenever I find a space ( ' ' ), I replace it with a comma plus space ( ', ' )

  • Thanks for the help! Look just like I was trying: str_replace($Description.' ' , $Description.',' , $Description); It should only be said where it will be changed in the last parameter, right?! rs

  • 1

    Haha happens friend. At first it’s always like this. But, since you’re starting with PHP, any questions about it, check this site here (I’ve already put it in the topic that talks about this method 'str_replace': http://php.net/manual/en/function.str-replace.php

3

I would do a little different, use regex to remove the additional spaces.

<?php
$exemplo = "texto de teste     com muitos   espaços talvez   erros de digitação";
$tags = preg_replace('/\s+/', ', ', $exemplo);
echo $tags . PHP_EOL;

$tags_2 = str_replace(" ", ", ", $exemplo);
echo $tags_2 . PHP_EOL;

Exits:

Output preg_replace texto, de, teste, com, muitos, espaços, talvez, erros, de, digitação

Output from str_replace source texto, de, teste, , , , , com, muitos, , , espaços, talvez, , , erros, de, digitação

Do you know the difference? I hope you helped him.

PS.: this is not the "correct way", it is only an alternative way, for when there are too many spaces.

-2

Based on your example, just do this:

$descricao = 'titulo de teste';  
//aqui ele limpa espaços duplos, e substitui por um espaço
$descricao = preg_replace('/\s+/', ' ',$descricao);  
$keywords = explode(' ',$descricao);
$listaComVirgula = implode(', ', $keywords);
echo $listaComVirgula;
  • Let’s face it .... it works but it gets too ugly right

  • The answer given is not to have beauty, it is to clarify the question. Ugliness is a matter of point of view.

  • Ivan not being offensive ... have you read anything about KISS ? I’m not saying your way is wrong, but why use 5 line if we can do in 1 ? it’s a matter of logic.

  • 1

    @Otto, "Kiss - Keep it simple stupid principle" I have read about this yes, I know where there is problem, and I know I could solve doing this: $descricao = preg_replace('/\s+/', ', ',$descricao);, but the case is abstract, I wanted to base myself on the question. Understood. For every question there are "n" answers that I could give, in this case it is more to show in practice the use of implode(). It didn’t seem offensive, but pretentious.

  • Guys, regardless of the way used to answer the question, it is worth highlighting your willingness to help, and I am happy to know that for case there are N possible solutions. I’m a beginner in PHP and have learned a lot here at stackoverflow. Anyway, thanks!

Browser other questions tagged

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