这个功能在这里有什么作用?

时间:2016-05-27 00:21:23

标签: php

我正在研究一些代码,我发现了这个:

$linkDirectToApp=preg_replace('/\s+/', '', 'https://example.com/'.$subdo);

preg_replace究竟对我的网址做了什么以及如何禁用它?

2 个答案:

答案 0 :(得分:3)

删除空格:
\s - 空白
+ - 1或更多
\s+ - 表示1个或多个空格。

要禁用它,只需更改您的行:

$linkDirectToApp = 'https://example.com/'.$subdo;

答案 1 :(得分:0)

$url = "this is a url";
echo $url."<br/>";
preg_replace('/\s+/', '', $url);
echo $url;
**The Output will  be** 
this is a url
this is a url

函数 preg_replace 不会就地修改字符串。它返回一个新字符串,其中删除了空格的结果。

$url1 = "this is a url";
echo $url1."<br/>";
$url2 = preg_replace('/\s+/', '', $url1);
echo $url2;
**The Output will  be** 
this is a url
thisisaurl

在这种情况下,它会删除此字符串(https://example.com/'。$ subdo)中的空格,其中为空,并将其分配给$ linkDirectToApp。

要停用 preg_replace ,请将变量替换为以下字符串。

$linkDirectToApp="https://example.com/$subdo";