如何从包含" \"的字符串中提取?用PHP

时间:2015-06-17 16:18:24

标签: php

我想从字符串MHadi中提取Corp\MHadi。 我使用以下代码,但每次都会出错。

$MyUser = "Corp\Mhadi";
$MyName = explode("\", $MyUser);
echo "Actual User = $MyUser <br />";
echo "First part = $MyName [0]<br />";
echo "Second part = $MyName [1]<br />";

2 个答案:

答案 0 :(得分:0)

“\”逃脱一个角色。只是做

$MyName = explode("\\",$MyUser);

它应该有用

$MyUser = "Corp\Mhadi";
$MyName = explode("\\", $MyUser);
echo "Actual User = ".$MyUser." <br />";
echo "First part = ".$MyName[0]."<br />";
echo "Second part = ".$MyName[1]."<br />";

答案 1 :(得分:0)

你必须转义\,因为这是php中的转义字符。

$MyUser = "Corp\Mhadi";
echo "Actual User = $MyUser <br />";
$MyName = explode('\\', $MyUser);
echo "First part = $MyName[0]<br />";
echo "Second part = $MyName[1]<br />";

这样就行了。