0

I have a large list which is populated with different types of feed urls for cameras, the URL structure is slightly different each time.

I need to strip out or replace the Segments/fields from the different URL types and create valid ones, I am not sure if this is even possible with regular expressions

Examples of URLS:

rtsp://IPADDRESS/
http://IPADDRESS/videostream.asf
http://IPADDRESS//iphone/11?[USERNAME]:[PASSWORD]&
http://IPADDRESS/img/video.mjpeg

Segments which always exist: IPADDRESS

Segments which are always the same (if they exist) IPADDRESS [USERNAME] [PASSSWORD]

The end results is I need to separate each segment into variables so I can use them to create a valid URL.

For example I will use this URL "http://IPADDRESS//iphone/11?[USERNAME]:[PASSWORD]" and replace the segments with valid data

EDIT:

The URLS are stored in a database, its not coming from the URL field in a browser.

I am attempting it using multiple str_replaces, but I am not sure how reliable this is, it might be better with a loop. But how would you break the url up using a loop:

$string = str_replace('IPADDRESS', '10.10.10.10', $string);
$string = str_replace('[USERNAME]', 'user1', $string);

http://10.10.10.10//iphone/11?user1:mypass

Thanks for your help

3
  • 1
    parse_url()? Commented Mar 6, 2014 at 22:38
  • Two things to remember: 1) Regexes are not a magic wand that you wave at any problem that happens to involve strings. 2) Common tasks like handling URL contents usually have existing functions to take care of them. Commented Mar 7, 2014 at 1:37
  • 3) Those existing functions often use regexes, so... maybe they are a magic wand? Commented Mar 7, 2014 at 4:09

1 Answer 1

4

I think you're actually looking for parse_url().

This function parses a URL and returns an associative array containing any of the various components of the URL that are present.

Sign up to request clarification or add additional context in comments.

1 Comment

Thats very helpful, thanks I am looking into this now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.