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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:39:02+00:00 2026-06-01T17:39:02+00:00

I was looking to strip out base_url from input supplied via html input=text and

  • 0

I was looking to strip out base_url from input supplied via html input=text and pulled from _POST. The input itself is most likely expected to contain the full uri, but also/and quite possibly a port assignment followed by a few more path delimiters.

example: https://lab1.sfo1.transparentpixel.com:554/rtmp/_definst_

I needed up to 3 instantiations of the result and those values end up getting placed into an array.

So to test things in a stand alone script, I ended up with the following code:

OLD FOR HISTORICAL REVIEW:

<?php
$var1 = "https://lab1.sfo1.transparentpixel.com:1935/rtsp/_definst_";
$var2 = "http://lab1.sfo1.transparentpixel.com:1935/rtmp/_definst_";
$var3 = "lab1.sfo1.transparentpixel.com";

$count = 1;
while ( $count <= 3 )
{
$test[] = 'var'.$count.' = ' . preg_replace(array("#^.*/([^\:]+)\:.*#"), '$1', ${var.$count});
$count++;
}

var_dump($test);
?>

CORRECTED AFTER EDIT:

<?php

    $url1 = "https://lab1.sfo1.transparentpixel.com:1935/rtsp/_definst_";
    $url2 = "http://lab1.sfo1.transparentpixel.com:1935/rtmp/_definst_";
    $url3 = "lab1.sfo1.transparentpixel.com";

$count = 1;
while ( $count <= 3 )
{
$test[] = 'url'.$count.' = ' . preg_replace(array("#^.*/([^\:]+)\:.*#"), '$1', ${url.$count});
$count++;
}

print_r($test);
?>

My result:

$ php tpixel_url_replace.php 
Array
(
    [0] => url1 = lab1.sfo1.transparentpixel.com
    [1] => url2 = lab1.sfo1.transparentpixel.com
    [2] => url3 = lab1.sfo1.transparentpixel.com
)

While this works as I intended, I’m certainly missing some iterations. Anyone care to elucidate things I may be overlooking? Yes, I know I could have used str_replace but the cost of running preg_ over str_ is minimal in the overall scheme of things.

I’m simply looking for insight as I’m 100% sure I’m not a master of anything regarding reg-ex nor preg_replace.

Input?

  • 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-01T17:39:04+00:00Added an answer on June 1, 2026 at 5:39 pm

    I hope I understand your question correctly. Are you having trouble with the regex or the code for looping over the urls? Or both?

    I’m going to assume both…

    Instead of matching the whole thing and grouping the bit you want to extract, I’d suggest you match just what you want to extract. With that in mind, the regex could look like this:

    [^/]+\.[^/:]{2,3}

    In english this says:

    Match anything except a forward slash until there is a dot, then match between 2 and 3 more of anything except a forward slash or a colon

    This seems simple, but i think it gets you what you need.

    Here is a bit of php code that creates an array of urls in various formats and then loops though each one and extracts just the bit i think you want. I’ve switched to using preg_match instead of preg_replace because i think it makes more sense in this case:

    <?php
        $urls = array(
                    "https://lab1.sfo1.transparentpixel.co.jp:1935/rtsp/_definst_",
                    "http://lab1.sfo1.transparentpixel.com:1935/rtmp/_definst_",
                    "http://lab1.sfo1.transparentpixel.com/rtmp/_definst_",
                    "lab1.sfo1.transparentpixel.com",
                    "someurl.com:1935/rtmp/_definst_",
                    "someurl.com/_definst_",
                    "http://someurl.co.uk");
    
        foreach($urls as $url)
        {
            preg_match('%[^/]+\.[^/:]{2,3}%m', $url, $matches);         
            echo $matches[0]; // instead of this you could do $test[] = $matches[0];  
        }
    ?>
    

    You’ll notice that I’m looping over the array using a foreach loop which means we are not limited to a fixed number of iterations as in your example.

    The output of this is:

    lab1.sfo1.transparentpixel.co.jp
    lab1.sfo1.transparentpixel.com
    lab1.sfo1.transparentpixel.com
    lab1.sfo1.transparentpixel.com
    someurl.com
    someurl.com
    someurl.co.uk
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking to implement a system to strip out url's from text posted
I'm looking for a method (or function) to strip out the example.ext part of
I want to strip script tags out of this HTML at Pastebin: http://pastebin.com/mdxygM0a I
I'm looking for a way to convert HTML to text. I tried using sanatize
I'm looking to strip out non-numeric characters in a string in ASP.NET C#, i.e.
Looking to move data from a table A to history table B every X
I needed to strip the Chinese out of a bunch of strings today and
I'm trying to strip the surrounding tags that are put out by default for
I am looking for a Java Library to validate a URL and to strip
I'm looking for ActiveX components that can easily: get and send emails via SMTP

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.