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?
http://someurl.comorsomeurl.com:1935/rtmp/_definst_?