Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7551511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:30:53+00:00 2026-05-30T10:30:53+00:00

I’m basically formatting urls before sending my object to the view to loop through

  • 0

I’m basically formatting urls before sending my object to the view to loop through (with a foreach() on $submissions. The problem I’m having is that parse_url() takes a single index and not an entire array object.

I’ve got this method in my SubmissionsController:

public function newest() {        
    $submissions = $this->Submission->find('all', array(
        'conditions' => array('Submission.approved' => '1'),
        'order' => 'Submission.created DESC'
            ));

    $this->set('submissions', $submissions);
    $this->set('sourceShortUrl', AppController::shortSource($submissions));
}

In my AppController I’ve got this method which returns the formatted url:

protected function shortSource($source) {
    return $sourceShortUrl = str_ireplace('www.', '', parse_url($source, PHP_URL_HOST));
}

This works for single entries, but parse_url can’t take arrays, so is there a way in the controller to send the index of the object? E.g. $submissions[‘Submission’][‘source’] before I loop through it in the view?

My alternative was to do something like this in my shortSource($source) method:

if (is_array($source)) {
    for ($i = 0; $i < count($source); $i++) {
        return $sourceShortUrl = str_ireplace('www.', '', parse_url($source[$i]['Submission']['source'], PHP_URL_HOST));
    }
}

But that’s just returning the first (obviously). What is the best way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:30:54+00:00Added an answer on May 30, 2026 at 10:30 am

    You’re on the right track. Check for an array. If it’s an array, call it recursively for each item in the array.

    /**
    * shortSource
    *
    * Returns an array of URLs with the www. removed from the front of the domain name.
    * 
    * @param mixed $source Either a string or array
    * @return mixed $sourceShortUrl An array of URLs or a single string
    */
    protected function shortSource($source) {
        if (is_array($source)) {
            foreach ($source as $url) {
                $sourceShortUrl[] = $this->shortSource($url);
            }
        } else {
            $sourceShortUrl = str_ireplace('www.', '', parse_url($source, PHP_URL_HOST));
        }
        return $sourceShortUrl;
    }
    

    In this recursive function, it will parse a single string or an array of strings.

    // in the view
    if (is_array($sourceShortUrl)) {
        foreach ($sourceShortUrl as $url) {
            // view specific code for URL here
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.