简体   繁体   中英

how to remove space and replace forward slash in php

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 "AP-20014-03-12-4098"

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);
                          ^    ^           ^    ^
                          |----|-----------|    |
                               |----------------|

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;
$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

You can do something like this

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

It's possible to add aa 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM