user2562455
user2562455

Reputation: 33

Formatting url strings PHP array

Hi all I am sorry if this is a dumb question and I understand I might get banned for asking it but after a lot of work reading over PHP manual, reading the relevant chapters in PHP5.3 and scowering across Stackoverflow I am stuck in my tracks.

I have been universally format the url's taken in from a Search API I have tried to use parse_url(), trim and others unsuccessfully I decided upon str_replace

foreach ($jsonObj->RESULT as $value) {

 $BLekko_results[] = array(
                            'url' => strip_tags($value->url),
            'url' => str_replace("http://www.", "http://", $value->url),
                    'url' => str_replace("https://www.", "http://", $value->url),
                     'url' => str_replace( " http://", "http://", $value->url),
                     'url' => str_replace( " http://", "http://", $value->url),
                      title' => $value->url_title,); }

I plead humbly for you help ...

Upvotes: 0

Views: 407

Answers (3)

Yogesh
Yogesh

Reputation: 924

$BLekko_results = array();
foreach ($jsonObj->RESULT as $value) {
    $value = strip_tags($value->url);
    $updatedURL = str_replace(array('http://www.','https://www.','https://'),"http://",$value->url);
    $updatedTitle = $value->url_title;
    $BLekko_results[] = array('url'=>$updatedURL,'title'=>$updatedTitle);
}
echo "<pre>";
print_r($BLekko_results);
echo "</pre>";

Upvotes: 0

Expedito
Expedito

Reputation: 7795

foreach ($jsonObj->RESULT as $value) {
    $url = trim($value->url);
    $find = array("http://www.", "https://www.", "https://");
    $BLekko_results[] = array(
        'url' => str_replace($find, "http://", $url),
        'title' => $value->url_title,);
}

Upvotes: 1

Brannon
Brannon

Reputation: 1336

Perhaps try something like this:

public function processURLString($urlString) {
    $urlString = trim($urlString);

    if($urlString) {
        $urlString = preg_replace('/https?:\/\//', '', $urlString);
        $urlString = trim($urlString);
        $urlString  = 'http://'.$urlString;
    }

    return $urlString;
}

And then you can add or remove www etc...

Upvotes: 1

Related Questions