PDA

View Full Version : Regular Expression help needed


kaisellgren
09-22-2006, 12:58 PM
Hi,

I would like to convert "http://www.sdgsg.com/dfgdfg/sdfd" to "dfgdfg/sdfd". I have done this so far:

$path = "http://www.localhost.com/dffg/ks";
$path = str_ireplace("http://","",$path);
$path = preg_replace("/.*\//","",$path);

But the only problem is that it returns me "ks", but it should return "dffg/ks"... how do I make the preg_replace command to stop after first match?

timgolding
09-22-2006, 01:03 PM
not sure but if you get really stuck try the explode function.

kehers
09-22-2006, 01:31 PM
Yeah...exploding the path may be the easier way out

<?php
$path = "http://www.sdgsg.com/dfgdfg/sdfd";
$path_array = explode("/", $path);
/*//the content of ur array here
echo "<pre>";
print_r($path_array);
echo "</pre>";
*/
//you most probably will be needing the keys 3 and 4
$path = $path_array[3]."/".$path_array[4];
//echo $path;
?>

marek_mar
09-22-2006, 01:59 PM
parse_url() (http://www.php.net/manual/en/function.parse-url.php)

kaisellgren
09-22-2006, 02:00 PM
Thanks I made it with explode();