[PHP] Resolve a relative url from 1 file to another given the 2 files paths

Using PHP, is there any way to easily resolve a relative url from one path to another?

file1: sub/file1.html
file2: file2.html
result (from file1): …/file2.html

I can parse it myself by going through the path and comparing the parts, but I was wondering if there was some other way or some method that did this automatically. Any ideas?