You can use a combination of parse_url()
and regex to accomplish this. The below regular expression will strip out everything after the index.php
in the URL path.
$parts = parse_url($url);
$scriptname = preg_replace('#(index\.php)/.*#', '$1', $parts['path']);
$result = $parts['scheme'].'://'. $parts['host'] . $scriptname;
For the two URLs given the question, the output would be as follows:
http://testing.org/directory/index.php
http://testing.org/index.php