为什么 PHP 更改 ltrim 或 str_replace 之后的第一个字符?
Why is PHP changing the first character after ltrim or str_replace?
我正在尝试使用 PHP ltrim() 删除目录的一部分,但结果出乎意料。我的结果的第一个字母有错误的 ascii 值,并且在浏览器中显示为缺少 character/box。
这是我的代码:
$stripPath = "public\docroot00-4399\computer-system-upgrade";
$directory = "public\docroot00-4399\computer-system-upgrade.0 Outgoing Documents";
$shortpath = ltrim($directory, $stripPath);
echo $shortpath;
预期输出:
3.0 Outgoing Documents
实际输出:
.0 Outgoing Documents
注意点之前的 invisible/non-print 字符。 Ascii 值从 Hex 33(数字 3)更改为 Hex 03(不可见字符)。
我还尝试了 str_replace() 而不是 trim(),但结果保持不变。
我在这里做错了什么?我将如何获得预期结果“3.0 传出文件”?
这是因为 /
标记,因为 /
具有特殊含义。
如果您使用 space </code> 尝试此操作,您可以获得预期的输出。</p>
<pre><code>$stripPath = "public\docroot00-4399\computer-system-upgrade";
$directory = "public\docroot00-4399\computer-system-upgrade 3.0 Outgoing Documents";
$shortpath = ltrim($directory, $stripPath);
echo $shortpath;
当您在引号中提供字符串值时,您必须注意反斜杠用作屏蔽字符。因此,</code> 被理解为 ASCII(3) 字符。在您的示例中,您需要提供双反斜杠才能定义所需的字符串(其中包含单反斜杠):</p>
<pre><code>$stripPath = "public\docroot\4300-4399\computer-system-upgrade\";
$directory = "public\docroot\4300-4399\computer-system-upgrade\3.0 Outgoing Documents";
反斜杠是PHP的转义序列。将反斜杠添加到‘stripPath’到trim它来自‘dirctory’
不要使用 ltrim。
Ltrim 不会直接替换。它的剥离有点像正则表达式。
这意味着您在第二个参数中输入的所有字符都用于删除任何内容。
参见示例:https://3v4l.org/AfsHJ
它停在 .
的原因是因为它不是 $stripPath
的一部分
您应该改为使用真正的正则表达式或简单的 str_replace。
$stripPath = "public\docroot00-4399\computer-system-upgrade";
$directory = "public\docroot00-4399\computer-system-upgrade.0 Outgoing Documents";
$shortpath = str_replace($stripPath, "", $directory);
echo $shortpath;
我正在尝试使用 PHP ltrim() 删除目录的一部分,但结果出乎意料。我的结果的第一个字母有错误的 ascii 值,并且在浏览器中显示为缺少 character/box。
这是我的代码:
$stripPath = "public\docroot00-4399\computer-system-upgrade";
$directory = "public\docroot00-4399\computer-system-upgrade.0 Outgoing Documents";
$shortpath = ltrim($directory, $stripPath);
echo $shortpath;
预期输出:
3.0 Outgoing Documents
实际输出:
.0 Outgoing Documents
注意点之前的 invisible/non-print 字符。 Ascii 值从 Hex 33(数字 3)更改为 Hex 03(不可见字符)。 我还尝试了 str_replace() 而不是 trim(),但结果保持不变。
我在这里做错了什么?我将如何获得预期结果“3.0 传出文件”?
这是因为 /
标记,因为 /
具有特殊含义。
如果您使用 space </code> 尝试此操作,您可以获得预期的输出。</p>
<pre><code>$stripPath = "public\docroot00-4399\computer-system-upgrade";
$directory = "public\docroot00-4399\computer-system-upgrade 3.0 Outgoing Documents";
$shortpath = ltrim($directory, $stripPath);
echo $shortpath;
当您在引号中提供字符串值时,您必须注意反斜杠用作屏蔽字符。因此,</code> 被理解为 ASCII(3) 字符。在您的示例中,您需要提供双反斜杠才能定义所需的字符串(其中包含单反斜杠):</p>
<pre><code>$stripPath = "public\docroot\4300-4399\computer-system-upgrade\";
$directory = "public\docroot\4300-4399\computer-system-upgrade\3.0 Outgoing Documents";
反斜杠是PHP的转义序列。将反斜杠添加到‘stripPath’到trim它来自‘dirctory’
不要使用 ltrim。
Ltrim 不会直接替换。它的剥离有点像正则表达式。
这意味着您在第二个参数中输入的所有字符都用于删除任何内容。
参见示例:https://3v4l.org/AfsHJ
它停在 .
的原因是因为它不是 $stripPath
您应该改为使用真正的正则表达式或简单的 str_replace。
$stripPath = "public\docroot00-4399\computer-system-upgrade";
$directory = "public\docroot00-4399\computer-system-upgrade.0 Outgoing Documents";
$shortpath = str_replace($stripPath, "", $directory);
echo $shortpath;