Click to See Complete Forum and Search --> : shorten url
zebdaag
11-23-2006, 11:50 AM
hi i want to shorten some urls that i take from my database i got this script but it doesn't work:
<?
$text = "http://www.uni-regensburg.de/Fakultaeten/philFakII/Psychologie/PsyII/beautycheck/english/index.htm";
$chars = 25;
$niew = $text." ";
$niew = substr($niew,0,$chars);
$niew = substr($niew,0,strrpos($niew,' '));
$niew = $niew."...";
echo $niew;
?>
this outputs only the ...
how to solve this??
thanks in advance!
Greetz Joost
so_is_this
11-23-2006, 01:13 PM
How about this?
$url = "http://www.uni-regensburg.de/Fakultaeten/philFakII/Psychologie/PsyII/beautycheck/english/index.htm";
echo preg_replace('\/(\w+\/)+', '/.../', $url);
bokeh
11-23-2006, 02:08 PM
echo preg_replace('/\/(\w+\/)+/', '/.../', $url);I think I'd write that a bit differently just in case there are any non-word characters in the string.echo preg_replace('|(?!</)/([^/]+/)+|', '/.../', $url);
so_is_this
11-23-2006, 02:43 PM
Yours replaces incorrectly (with those | in there)
or replaces too much (without them).
Without those | in yours, I get this:
http://.../index.htm
With mine, I get this:
http://www.uni-regensburg.de/.../index.htm
bokeh
11-23-2006, 03:42 PM
Yours replaces incorrectlyIt was a typo in the expression. I didn't test it. Should have been:echo preg_replace('|(?<!/)/([^/]+/)+|', '/.../', $url);
so_is_this
11-23-2006, 05:52 PM
What are the vertical bars on both ends supposed to do in your suggestion? My understand is that those are supposed to be like "or" operators (alternative branch) -- meaning there should be a pattern on both sides of them. Perhaps you can teach me something by telling me how they are supposed to work in your suggestion. Because you're suggestion is still not working for me.