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 7163825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:58:04+00:00 2026-05-28T13:58:04+00:00

I have a PHP function which takes a passed url and creates a clean

  • 0

I have a PHP function which takes a passed url and creates a clean link. It puts the full link in the anchor tags and presents just “www.domain.com” from the url. It works well but I would like to modify it so it strips out the “www.” part as well.

<?php
    // pass a url like: http://www.yelp.com/biz/my-business-name
    // should return: <a href="http://www.yelp.com/biz/my-business-name">yelp.com</a>
    function formatURL($url, $target=FALSE) {
        if ($target) { $anchor_tag = "<a href=\"\\0\" target=\"$target\">\\4</a>"; }
        else { $anchor_tag = "<a href=\"\\0\">\\4</a>"; }
        $return_link = preg_replace("`(http|ftp)+(s)?:(//)((\w|\.|\-|_)+)(/)?(\S+)?`i", $anchor_tag, $url);
        return $return_link;
    }
?>

My regex skills are not that strong so any help 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-05-28T13:58:04+00:00Added an answer on May 28, 2026 at 1:58 pm

    Take a look at parse_url: https://www.php.net/manual/en/function.parse-url.php

    This will simplify your logic quite a bit can can make replacing the www. a simple string replace.

    $link = 'http://www.yelp.com/biz/my-business-name';
    $hostname = parse_url($link, PHP_URL_HOST));
    if(strpos($hostname, 'www.') === 0)
    {
       $hostname = substr($hostname, 4);
    }
    

    I have modified my original answer to account for the issue in the comments. The preg_replace in the post below will also work and is a bit more concise, I will leave this here to show an alternative solution that does not require invoking the regex engine if you desire.

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

Sidebar

Related Questions

Is it possible to have a function in PHP which takes 1 or more
I have a PHP function on my site which takes a couple of seconds
i have a php function which returns a random json encoded color <?php function
I have a load of php templates which uses a custom translate function __,
I have a PHP function that takes a variable number of arguments (using func_num_args()
Right now I have a function which takes my uploaded file, checks the extension,
So I have a PHP class called router that takes the URL and explodes
I have a function which sends specified form data to a php processing page.
I have a PHP script that loads a function which uses Curl to log
I have a fairly simple function in Delphi which takes a string and produces

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.