Concatenate letters / words if it contains "&" and there is space between them

1

How to concatenate letters / words if it contains "&" and there is space between them?

Example:

A & E COMERCIO DE ALGUMA COISA LTDA
EMPRESA P & C CONSTRUCAO LTDA
CAMARAO & CIA FULANO DE TAL - ME

// OUTPUT
A&E COMERCIO DE ALGUMA COISA LTDA
EMPRESA P&C CONSTRUCAO LTDA
CAMARAO&CIA FULANO DE TAL - ME
    
asked by anonymous 05.05.2015 / 20:44

2 answers

5

You can use preg_replace to replace the occurrence of  &  .

preg_replace("/ *& */", "&", $string);

With this regex will replace spaces that exist before & after &.

  

*: indicates that the previous parameter occurs once or several times.

     

In this case, we have that can exist: AS & DFG, AS & DFG, AS & DFG, AS & DFG, in all these cases it will match and execute replace.

Example:

$example = "A   &   E COMERCIO DE ALGUMA COISA LTDA";
$out = preg_replace("/ *& */", "&", $example);
echo $out; // A&E COMERCIO DE ALGUMA COISA LTDA

preg_replace

    
05.05.2015 / 20:51
0

I do not understand PHP, but try it there:

A 0 & 0 E COMERCIO DE ALGUMA COISA LTDA COMPANY P & C CONSTRUCTION LTD

or

A \s & \s E COMERCIO DE ALGUMA COISA LTDA COMPANY P & C CONSTRUCTION LTD

    
05.05.2015 / 20:49