[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Shrink a URL

Posted on 2008-06-10
3
Medium Priority
?
3,173 Views
Last Modified: 2008-06-25
This is probably going to be pretty basic for anyone that is good with regular expressions.  Unfortunately I am not.  I have tried, but the only solution I came up with didn't work right and took up a bunch of lines of code to do it.

The language I need to do this in is PHP.  I would prefer to do this in preg instead of ereg since ereg is supposed to be depreciated in the next version of PHP.

I want to take a URL and "shrink" it if it is over a certain number of characters.

This is just an example URL that I saw that is too long:

www.engadget.com/2008/06/09/worlds-fastest-ibms-roadrunner-supercomputer-breaks-petaflop/

What I want to do is if the URL is over 80 characters I want to try and shrink it down.  The above URL should become:

www.engadget.com/.../worlds-fastest-ibms-roadrunner-supercomputer-breaks-petaflop/

So it would keep the first section and the last section.

If it i still over 80 characters, I'd like for it to then go into the final section, the above would become:

www.engadget.com/.../...s-fastest-ibms-roadrunner-supercomputer-breaks-petaflop/

It would go into the final section removing characters until it got it so that the string was 80 characters, including the ellipsis (...).

Any ideas?  If you post code that does this, please explain how the code works.  I want to learn from this, not just get a block of code that works.

Thanks for any insight that you may provide in advance.
0
Comment
Question by:jstrellner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 4

Expert Comment

by:afzz
ID: 21752081
Not a Regex but I have used a function to achieve something like this. Check the code below if it helps you.
<?
$url = "www.engadget.com/2008/06/09/worlds-fastest-ibms-roadrunner-supercomputer-breaks-petaflop/";
 
echo trim_url($url,80);
 
function trim_url($url,$max){
if(strlen($url)>$max){
$tmp=explode("/",$url);
	if((strlen($tmp[0] . "/.../" . $tmp[count($tmp)-1])) <= $max){
		if(($max - (strlen($tmp[0])+5)) > 0){
			$str="";
			$i = count($tmp);
			while($i>=1){
				$i = $i - 1;
				if($trail == 1){
				$str = $tmp[$i].$str;
				$trail = 0;
				}
				elseif($str != "")
				$str = $tmp[$i]."/".$str;
				else
				$str = $tmp[$i];
				if(($tmp[$i]=="")&&($str=="")){
				$str = "/";
				$trail=1;
				}
				if((strlen($str)+strlen($tmp[0])+8) > $max){
					return $tmp[0]."/.../...".substr($str,strlen($tmp[0])+8,$max-(strlen($tmp[0])));
					break;
					}
				}
		
		}
		else{
		return $tmp[0]."/.../";
		}
		
	}
	else{
	return $tmp[0]."/.../".$tmp[count($tmp)-1];
	}
 
}
else{
return $url;
}
}
?>

Open in new window

0
 
LVL 1

Author Comment

by:jstrellner
ID: 21753668
I took what I had before and added one or two things from what I saw in your code.  For some reason when I used your code, I got URLs that were sometimes longer than the $max I passed to it.

In the code below, which I tried to document well so you can follow it, it will try to get it as close to the $max as possible if it is longer than $max.  It will only cut into the final part of the URL if it has to, and in that case, the result will be exactly what the $max is set to.  If it can get under the $max by removing folders, it will.

I will leave this question open for a few more days in case someone can post something more efficent that the snippit I am including below.  If no one posts a better snippet than I will assign you partial points afzz.
function Shrink_URL($url, $max) {
	if (strlen($url)>$max) {
		$tmp=explode("/",$url);
		if ((strlen($tmp[0] . "/.../" . $tmp[count($tmp)-1])) >= $max) {
			// the domain plus the ellipsis and the last part are greater than $max
			
			// check to see if we are smaller than $max without the last part
			if ((strlen($tmp[0] . "/.../")) < $max) {
				// we are smaller, lets shrink the last part to make us be our $max
				
				// $trimchars is the number of characters to remove.
				$trimchars = (strlen($tmp[0] . $tmp[count($tmp)-1])+8)-$max;
				return $tmp[0] . "/.../..." . substr($tmp[count($tmp)-1], $trimchars);
			}
			else {
				// we are not smaller, lets just return this.  max length of domain = 64 + www. (4) = 68. 
				// As long as $max is bigger than 68 this won't happen typically. 
				return $tmp[0]."/.../";
			}
 
		}
		else {
			// the domain plus the ellipsis and the last part are smaller
			// now lets try to get it as close to the $max as possible.
			
			// see how many parts we have
			$parts = count($tmp);
			if ($parts > 2) {
				foreach ($tmp AS $id=>$tmp_part) {
					// if its the first or last item, skip it.
					if (($id == 0) OR ($id == (count($tmp)-1))) continue;
					if ((strlen($tmp[0] . $added_parts . '/' . $tmp_part . "/.../" . $tmp[count($tmp)-1])) >= $max) {
						// we are too big now, lets return what we have
						return $tmp[0] . $added_parts . "/.../" . $tmp[count($tmp)-1];
					}
					else {
						$added_parts .= '/' . $tmp_part;
					}
				}
			}
			else {
				// there were only two parts.  We can't do anything
				return $tmp[0] . "/.../" . $tmp[count($tmp)-1];
			}
		}
	}
	else{
		return $url;
	}
}

Open in new window

0
 
LVL 1

Accepted Solution

by:
jstrellner earned 0 total points
ID: 21753888
I made one minor change to fix it when the URL ends with a trailing slash.

The new addition is on line #2 there is now a regex that removes a trailing slash if it exists.

Everything else is the same, but pushed down one line.
function Shrink_URL($url, $max) {
	$url = preg_replace('/\/$/', '', $url);
	if (strlen($url)>$max) {
		$tmp=explode("/",$url);
		if ((strlen($tmp[0] . "/.../" . $tmp[count($tmp)-1])) >= $max) {
			// the domain plus the ellipsis and the last part are greater than $max
			
			// check to see if we are smaller than $max without the last part
			if ((strlen($tmp[0] . "/.../")) < $max) {
				// we are smaller, lets shrink the last part to make us be our $max
				
				// $trimchars is the number of characters to remove.
				$trimchars = (strlen($tmp[0] . $tmp[count($tmp)-1])+8)-$max;
				return $tmp[0] . "/.../..." . substr($tmp[count($tmp)-1], $trimchars);
			}
			else {
				// we are not smaller, lets just return this.  max length of domain = 64 + www. (4) = 68. 
				// As long as $max is bigger than 68 this won't happen typically. 
				return $tmp[0]."/.../";
			}
 
		}
		else {
			// the domain plus the ellipsis and the last part are smaller
			// now lets try to get it as close to the $max as possible.
			
			// see how many parts we have
			$parts = count($tmp);
			if ($parts > 2) {
				foreach ($tmp AS $id=>$tmp_part) {
					// if its the first or last item, skip it.
					if (($id == 0) OR ($id == (count($tmp)-1))) continue;
					if ((strlen($tmp[0] . $added_parts . '/' . $tmp_part . "/.../" . $tmp[count($tmp)-1])) >= $max) {
						// we are too big now, lets return what we have
						return $tmp[0] . $added_parts . "/.../" . $tmp[count($tmp)-1];
					}
					else {
						$added_parts .= '/' . $tmp_part;
					}
				}
			}
			else {
				// there were only two parts.  We can't do anything
				return $tmp[0] . "/.../" . $tmp[count($tmp)-1];
			}
		}
	}
	else{
		return $url;
	}
}

Open in new window

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question