简体   繁体   English

如何删除空间并替换PHP中的正斜杠

[英]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" 我想要这样的结果“ AP-20014-03-12-4098”

A simple str_replace can do this: 一个简单的str_replace可以做到这一点:

$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. 可以使用要替换的字符/字符串向str_replace添加一个数组。

If you want to replace more characters you can just add it to the arrays 如果要替换更多字符,可以将其添加到数组中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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