0

i have variable

$var = "A/P/ 20014/03 /12/4098 "

space uncertain in variable how to remove space and replace forward slash. i want result like this "A-P-20014-03-12-4098"

Arif Hidayat
  • 67
  • 1
  • 9

4 Answers4

6

A simple str_replace can do this:

$var = "A/P/ 20014/03 /12/4098 ";
$var = str_replace(array('/', ' '), array('-', ''), $var);
echo $var;

Illustration:

                        search for        replacement
$var = str_replace(array('/', ' '), array('-', ''), $var);
                          ^    ^           ^    ^
                          |----|-----------|    |
                               |----------------|
Kevin
  • 41,694
  • 12
  • 53
  • 70
1

Use this:

$var = "A/P/ 20014/03 /12/4098 ";
// / to -
$var = preg_replace("/\//",'-',$var);
// removes all the whitespaces
$var = preg_replace('/\s+/', '', $var);
echo $var;
Traian Tatic
  • 681
  • 5
  • 18
0
$var = "A/P/ 20014/03 /12/4098 ";    // your string
$out = str_replace("/", "-", $var);  // replace all / with -
$string = preg_replace('/\s+/', '', $out);  // trim all white spaces
Jenz
  • 8,280
  • 7
  • 44
  • 77
0

You can do something like this

$var = str_replace(array(" ","/"), array("","-"), $var);

It's possible to add a a array to str_replace with the chars/strings you want to replace.

If you want to replace more characters you can just add it to the arrays