修剪在PHP中表现不一致

时间:2015-02-05 18:37:39

标签: php

我有一系列电子邮件ID - 它们采用以下格式

<my_link_1@mysite.com>
<my_link_35@mysite.com>
<my_link_40@mysite.com>

然后我使用foreach循环并解析每个电子邮件ID,如下面的函数所示

protected function getLinkIds($emailAddresses = array()) {
    $links = array();

    foreach ($emailAddresses as $email) {
        $username = substr($email, 0, strpos($email, '@'));
        if (false === strpos($username, 'my_link_'))) {
            continue;
        }

        CakeLog::write('debug', 'getLinkIds - username : '.$username);
        CakeLog::write('debug', 'getLinkIds - trim(username) : ' . trim($username, '<>"'));

        $linkstr = explode('my_link_', $username);
            $links[trim($username, '<>"')] = $linkstr[1];
        }

        return $links;
}

我期待一个看起来像下面的数组

[my_link_1] => 1
[my_link_35] => 35
[my_link_40] => 40

但我得到一个如下所示的数组

[my_link_1] => 1
[<my_link_35] => 35
[<my_link_40] => 40

由于某种原因,修剪doest没有修剪左侧插入符号超出第一个电子邮件ID - 令人费解!

3 个答案:

答案 0 :(得分:0)

你可以这样做:

$emails = array(
    '<my_link_1@mysite.com>',
    '<my_link_35@mysite.com>',
    '<my_link_40@mysite.com>'
);

$result = array();

foreach($emails as $email) {
    $email = trim($email, '<>');
    $tmp = strstr($email, '@', true);
    preg_match('/_([0-9]*)$/', $tmp, $matches);
    $result[$tmp] = $matches[1];
}

print_r($result);

结果:

Array
(
    [my_link_1] => 1
    [my_link_35] => 35
    [my_link_40] => 40
)

答案 1 :(得分:0)

http://codepad.viper-7.com/lN8WpI

修剪工作正常。看看上面的链接。

但这可能是最干净的方法:

DEMO

$email = "<my_link_40@mysite.com>";

$email = trim("<my_link_40@mysite.com>", "<>"); //my_link_40@mysite.com

$email = current(explode("@", $email)); //my_link_40

$email_int = (int) filter_var($email, FILTER_SANITIZE_NUMBER_INT); //40

$array = array( $email => $email_int );

var_dump($array);

array(1) 
{
    ["my_link_40"] => int(40)
}

答案 2 :(得分:0)

就像我在你的问题的评论中提到的那样,使用:

var_dump($emailAddresses);

要确保其他行不以空格开头,否则修改会在代码示例中失败,因为您没有任何设置为删除空格的修剪。

您可以通过以下方式完成此操作,以确保它不会以代码中的空格开头:

protected function getLinkIds($emailAddresses = array())
{
    $links = array();
    foreach ($emailAddresses as $email)
    {
        $email = trim($email);
        //.... rest of your code
    }
    return $links;
}

或者甚至如此简单:

protected function getLinkIds($emailAddresses = array())
{
    $links = array();
    foreach ($emailAddresses as $email)
    {
        $username = trim(substr($email, 0, strpos($email, '@')), ' <');
        //.... rest of your code
    }
    return $links;
}

以下是评论中OP回答之前的原始答案。

你几乎就在那里,当你删除用户名时,你剩下的就是最初的<,你可以通过改变这一行删除它:

$username = substr($email, 0, strpos($email, '@'));

添加 trim ,如下所示:

$username = trim(substr($email, 0, strpos($email, '@')), ' <');

现在您已经离开了my_link_#,您可以再次使用简单的left trim(ltrim)来获得您想要的内容:

$id = ltrim($username, 'my_link_');

现在你已经#了,所以你的代码看起来像是:

protected function getLinkIds($emailAddresses = array())
{
    $links = array();
    foreach ($emailAddresses as $email)
    {
        $username = trim(substr($email, 0, strpos($email, '@')), ' <');
        if (false === strpos($username, 'my_link_'))) {
            continue;
        }

        CakeLog::write('debug', 'getLinkIds - username : '.$username);
        CakeLog::write('debug', 'getLinkIds - trim(username) : ' . trim($username, '<>"'));

        $links[$username] = ltrim($username, 'my_link_');
    }
    return $links;
}

输出:

Array
(
    [my_link_1] => 1
    [my_link_35] => 35
    [my_link_40] => 40
)

Live DEMO