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

  • Home
  • SEARCH
  • 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 8715021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:44:47+00:00 2026-06-13T05:44:47+00:00

I am having some trouble performing the following operation.. http://www.google.com –> www.google.com/ https://google.com –>

  • 0

I am having some trouble performing the following operation..

http://www.google.com –> http://www.google.com/
https://google.com –> http://www.google.com/
google.com –> http://www.google.com/

I am trying to remove https:// or http://, ensure that www. is added to the beginning of the URL, and then add a trailing slash to the URL if it does not exist.

Feeling like I’ve gotten the majority of this figured out but I can’t get str_replace() to work how I’d like it to.

To my understanding this is how to use str_replace:

$string = 'Hello friends';
str_replace('friends', 'enemies', $string);
echo $string;
// outputs 'Hello enemies' on the page

Here is what I have so far:

$url = 'http://www.google.com';

echo reformat_url($url);

function reformat_url($url) {
    if ( substr( $url, 0, 7 ) == 'http://' || substr( $url, 0, 8 ) == 'https://' ) { // if http:// or https:// is at the beginning of the url
        $remove = array('http://', 'https://');
        foreach ( $remove as $r ) {
            if ( strpos( $url, $r ) == 0 ) {
                str_replace($r, '', $url); // remove the http:// or https:// -- can't get this to work
            }
        }
    }
    if ( substr( $url, 0, 4 ) != 'www.') { // if www. is not at the beginning of the url
        $url = 'www.' . $url; // prepend www. to the beginning
    }
    if ( substr( $url, -1 ) !== '/' ) { // if trailing slash does not exist
        $url = $url . '/';  // add trailing slash
    }
    return $url; // return the formatted url
}

Any assistance on a way to format the URL would be greatly appreciated; also I’m more curious regarding what I am doing wrong with str_replace to remove http:// or https://. If anyone could offer some insight as to what I am doing incorrectly it’d be greatly appreciated.

  • 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-06-13T05:44:48+00:00Added an answer on June 13, 2026 at 5:44 am

    Do

    $url = str_replace($r, '', $url);
    

    instead of

    str_replace($r, '', $url);
    

    because str_replace returns a new string; it doesn’t change $url.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

having some trouble with this can't understand why it won't work properly. http://host.philmadelphia2.com/~clare/order/ when
Having some trouble with some SQL. Take the following result for instance: LOC_CODE CHANNEL
Having some trouble with the following code. I've created a class to manage the
I am having trouble in performing some activity when a call interrupt comes on
Having some trouble with this code. Trying to return a tuple of tuples (coordinates)
Having some trouble trying to put together a page where images are placed properly.
I am having some trouble trying to simplify some logic that is contained in
having some trouble trying to figure this out, complete novice when it come to
Hey having some trouble trying to maintain transparency on a png when i create
I am having some trouble with the Google Maps API . I have an

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.