tommyg
tommyg

Reputation: 95

how do I remove this substring from every value in an array?

Im trying to use str_replace to remove this little piece from an array of links. This is what I have so far...

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>";
    }   
}

I keep getting results that look like this

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

Im trying to remove the '">' portion. What am I missing? Thx

Upvotes: 0

Views: 67

Answers (2)

CS GO
CS GO

Reputation: 912

<?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 );

?>

Upvotes: 0

Kevin
Kevin

Reputation: 41885

You need to assign the replacement to complete the changes:

With reference &

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>";
    }   
}

or

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>";
    }   
}

Upvotes: 1

Related Questions