ben_eire
ben_eire

Reputation: 35

Find and replace within an associated Array

So I have an associated array that returns search engine results, it returns a url,title and snippet for each result, I want to find an replace all instances of "http:// and www." from the beginning of each of the urls. This is what I've tried so far, it spits out the url,title and snippet but it doesn't replace the "http:// and www.",

<?php
foreach ($js->RESULT as $item)
    {   
        $blekkoArray[$i]['url'] = ($item->{'url'});         
        $blekkoArray[$i]['title'] = ($item->{'url_title'});
        $blekkoArray[$i]['snippet'] = ($item->{'snippet'});
        $i++;
    }

    $find = array ('http://','www.');
    $replace = array ('','');

    $new_blekkoArray = str_replace ($find, $replace, $blekkoArray);

    print_r ($new_blekkoArray); 
?>  

I'm a bit of a noob at php can anyone help. Regards from Ireland

Upvotes: 2

Views: 90

Answers (1)

user399666
user399666

Reputation: 19889

Try:

$find = array ('http://','www.');

foreach ($js->RESULT as $item)
{   
        $blekkoArray[$i]['url'] = str_replace ($find, '', ($item->{'url'}) );         
        $blekkoArray[$i]['title'] = ($item->{'url_title'});
        $blekkoArray[$i]['snippet'] = ($item->{'snippet'});

        $i++;
}

print_r ($blekkoArray);   

Upvotes: 3

Related Questions