如何从数组中的每个值中删除此子字符串?

时间:2014-11-01 07:03:21

标签: php arrays replace

我试图使用str_replace从链接数组中删除这个小块。这就是我到目前为止......

foreach ($link_body as $key => $unfinished_link)
{
    // take that non-link piece off the end of each link
    if (stripos($unfinished_link, '">') !== false ) 
    {  
        str_replace('">',"", $unfinished_link);
    } 
    else {
        echo "<font color='#00FF66'>$unfinished_link</font><br>";
    }   
}

我不断获得看起来像这样的结果

http://detroit.cars.com/usedcars/1992-toyota-tercel-pos-fubar-great-condition/123559">

我试图删除'">'部分。我错过了什么? THX

2 个答案:

答案 0 :(得分:1)

您需要指定替换项以完成更改:

参考&

foreach ($link_body as $key => &$unfinished_link) {
    // take that non-link piece off the end of each link
    if (stripos($unfinished_link, '">') !== false) {  
        $unfinished_link = str_replace('">',"", $unfinished_link);
        echo $unfinished_link;
    } else {
        echo "<font color='#00FF66'>$unfinished_link</font><br>";
    }   
}

foreach ($link_body as $key => $unfinished_link) {
    // take that non-link piece off the end of each link
    if (stripos($unfinished_link, '">') !== false) {  
        $link_body[$key] = str_replace('">',"", $unfinished_link);
    } else {
        echo "<font color='#00FF66'>$unfinished_link</font><br>";
    }   
}

答案 1 :(得分:0)

<?php

    foreach ($link_body as $key => $unfinished_link)
    {

    if ( stripos($unfinished_link, '">') !== false )   
        echo str_replace('">',"", $unfinished_link);        // echo it 

    else
        echo "<font color='#00FF66'>$unfinished_link</font><br>";

    }

?>

OR

<?php

    foreach ( $link_body as $key => $unfinished_link )
    {

    if ( stripos($unfinished_link, '">') !== false )   
        $new_link[] = str_replace('">',"", $unfinished_link);  //get all the modified in array

    else
        echo "<font color='#00FF66'>$unfinished_link</font><br>";

    }

    var_dump( $new_link );

?>