Foreach循环仅适用于最后一项

时间:2015-04-02 13:50:23

标签: comments arrays foreach

我尝试做的是在评论中找到网址,然后针对数组检查这些网址。如果网址在数组中并且包含密钥数据'如果有一定值,我想用自定义文本替换注释中的每个URL。

除了更换部件外,我的一切都在工作。这是我到目前为止所做的。

评论

Lorem www.link1.com and ipsum dolor sit amet, http://link2.com consectetur
adipiscing elit. Maecenas http://link3.com eu tempor nibh.

函数正在使用的数组

Array
(
    [0] => Array
        (
            [status] => 200
            [data] => one
            [url] => www.link1.com
        )

    [1] => Array
        (
            [status] => 204
            [data] => 
            [url] => http://link2.com
        )

    [2] => Array
        (
            [status] => 200
            [data] => two
            [url] => http://link3.com
        )
)

功能

function check_the_status( $arrays, $comment ) {
    // Bad statis types
    $status = apply_filters( 'status_filter', array( 'one', 'two', 'three' ) );
    $modified = '';
    foreach( $arrays as $key => $array ){
        $url = $array['url'];
        $data = $array['data'];
        if( in_array( $data, $status ) ) {
            $modified = '<span>the new text</span>';
            $the_comment = str_replace( $url, $modified, $comment );
        }
    }
    return $the_comment;
}

输出

Lorem www.link1.com and ipsum dolor sit amet, http://link2.com consectetur
adipiscing elit. Maecenas "<span>the new text</span>" eu tempor nibh.

所以,这个函数发生了什么,它是否只将新文本应用于数组中的最后一个URL,但我希望它应用于第一个和最后一个URL&#39;因为它们都具有我在数组中检查的数据值。我认为foreach循环不是实现我想要的正确方法,但我不确定最好的方法。

1 个答案:

答案 0 :(得分:0)

问题似乎与以下行有关。每次迭代都会通过传递$ comment来重新开始:

$the_comment = str_replace( $url, $modified, $comment );

您应将其更改为:

$the_comment = str_replace( $url, $modified, $the_comment );

并在循环之前初始化$ the_comment:

$the_comment = $comment;

根据其他评论,这是工作代码。我添加了“粗体”样式,因此结果更加明显。

    $arrays = Array
    (
        0 => Array
            (
                'status' => 200,
                'data' => 'one',
                'url' => 'www.link1.com'
            ),

        1 => Array
            (
                'status' => 204,
                'data' => '',
                'url' => 'http://link2.com'
            ),

        2 => Array
            (
                'status' => 200,
                'data' => 'two',
                'url' => 'http://link3.com'
            )
    );

    function check_the_status( $arrays, $comment ) {
        // Bad statis types
        $status = apply_filters( 'status_filter', array( 'one', 'two', 'three' ) );
        // The class for targeting the url
        $span_class = apply_filters( 'status_class', 'sl-unsafe-link' );
        $modified = '';
        $the_comment = $comment;
        foreach( $arrays as $key => $array ){
            $url = $array['url'];
            $data = $array['data'];
            if( in_array( $data, $status ) ) {
                $modified = '<span style="font-weight:bold">'.$url.'</span>';
                $the_comment = str_replace( $url, $modified, $the_comment );
            }
        }
        return $the_comment;
    }

    $comment = 'Lorem www.link1.com and ipsum dolor sit amet, http://link2.com consectetur adipiscing elit. Maecenas http://link3.com eu tempor nibh.';
    $new = check_the_status($arrays, $comment);
    echo $new;

我在本地运行它,这是输出:

Lorem <span style="font-weight:bold">www.link1.com</span> and ipsum dolor sit amet, http://link2.com consectetur adipiscing elit. Maecenas <span style="font-weight:bold">http://link3.com</span> eu tempor nibh.