[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Wrap URL if it is too long

Posted on 2009-04-30
20
Medium Priority
?
352 Views
Last Modified: 2013-12-12
I am pulling a URL from a database field to display with a record. THe problem is, some of the URL's are very long and it skews the page. How do I get them to wrap in IE?
0
Comment
Question by:katlees
  • 5
  • 5
  • 4
  • +2
19 Comments
 
LVL 31

Expert Comment

by:Wayne Barron
ID: 24271874
ASP (or) PHP?
0
 
LVL 31

Expert Comment

by:Wayne Barron
ID: 24271928
If ASP Then

Change the    DRS("URLLink")
To your RecordSet and FieldName of the URL.
(Please read the notes in the code)

Good Luck
Carrzkiss
<%
this_descr = Drs("URLLink")
If len(this_descr) < 31 then ' Change this # to the length of characters that you want to display
 Response.write this_descr
else  ' change the 31 below to the same # as you have above.
   Response.write left(this_descr,31) & "..[<a href='"&DRS("URLLink")&"'>Click</a>]"
%>

Open in new window

0
 

Author Comment

by:katlees
ID: 24272070
It is written in PHP
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Expert Comment

by:Wayne Barron
ID: 24272093
why did you post in "ASP" if it is written in PHP?
0
 

Author Comment

by:katlees
ID: 24272106
I didnt know I did. Sorry
0
 
LVL 31

Expert Comment

by:Wayne Barron
ID: 24272131
not sure if this is right.
(Found a translation site to convert ASP to PHP.
Hope it works for you)

Carrzkiss
<? 
$this_descr=Drs["URLLink"]; // WARNING: assuming Drs is an external array
if (strlen($this_descr) < 31) {
  // Change this # to the length of characters that you want to display
  echo $this_descr;
}
else {
  // change the 31 below to the same # as you have above.
  echo substr($this_descr,0,31)."..[<a href='".DRS["URLLink"]."'>Click</a>]"; // WARNING: assuming DRS is an external array
}
?>

Open in new window

0
 
LVL 31

Expert Comment

by:Wayne Barron
ID: 24272140
This is the site I used to translate

http://www.dowdybrown.com/dbprod/convertASP/
0
 
LVL 28

Expert Comment

by:strickdd
ID: 24272951
if you determine the max characters per line that you want, you can perform a loop that will continue to take the substring() of the URL. You will need to put the <a href> tag before and close it after each line you create.
0
 

Author Comment

by:katlees
ID: 24273100
I have for code what is below
while ( $row = mysql_fetch_assoc($result) ) {
 $facility_id = $row['ID'];
$Email = $row['Email'];
$Website = $row['Website'];
 
  echo("<font class=\"style8\"><a href=\"http://$Website\" target=\"_blank\"><font color=\"blue\">$Website</font></a><br>");
  echo("<font class=\"style8\"><a href=\"mailto:$Email\"><font color=\"blue\">$Email</font></a>");

Open in new window

0
 
LVL 6

Expert Comment

by:birwin
ID: 24276229

<?
//If you don't care where it breaks on the url, use a simple wordwrap

$url="http://www.somedomain.com/Directory/Another_Directory/program_name.php";
$newtext = wordwrap($url, 40, "<br />", true);
echo "Basic wordwarp is:  <br>$newtext <br />";
//Where 40 is the maximum length you want the url to be and  <br /> is the break.
/*RESULT
http://www.somedomain.com/Directory/Anot
her_Directory/program_name.php
*/

 //If you want it to break on a specific character, say a slash, also use strpos

$break_length=strpos($url,"/",30); // finds the first slash after the length specified. (This assumes your url won't exceed two lines.)

// It is possible your that strpos may not find a slash, which would return a zero. Add this to preveint that.
$break_length=($break_length)? $break_length : 30;  // if a slash wasn't found, set an arbitrary length
$newtext = wordwrap($url, $break_length, "<br />", true);
echo " <br>wordwrap with break on slash break_length is <br>$newtext<br />";
/*RESULT
http://www.somedomain.com/Directory
/Another_Directory/program_name.php
*/

// of course you could get a VERY long url that exceeded your length, even with the break on the slash
 $url="www.somedomain.com/Directory/Another_Directory/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcitement.php";
$break_length=($break_length)? $break_length : 30;  // if a slash wasn't found, set an arbitrary length
$newtext = wordwrap($url, $break_length, "<br />", true);

//you can then do an absolute wordwrap on the wordwrapped variable
$newtext=wordwrap($newtext, 50,"\n");
echo " <br>wordwrap with break on slash with extra long program and forced break  is <br>$newtext<br />";
/*RESULT
www.somedomain.com/Directory/Another_Directory
/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
citement.php
 
?>

  Note the code snippet and this message are identical. I added the snippet since sometines the editor distorts the code.


<? 
//If you don't care where it breaks on the url, use a simple wordwrap 
 
$url="http://www.somedomain.com/Directory/Another_Directory/program_name.php"; 
$newtext = wordwrap($url, 40, "<br />", true); 
echo "Basic wordwarp is:  <br>$newtext <br />"; 
//Where 40 is the maximum length you want the url to be and  <br /> is the break. 
/*RESULT
http://www.somedomain.com/Directory/Anot
her_Directory/program_name.php 
*/
 
 //If you want it to break on a specific character, say a slash, also use strpos 
 
$break_length=strpos($url,"/",30); // finds the first slash after the length specified. (This assumes your url won't exceed two lines.)
 
// It is possible your that strpos may not find a slash, which would return a zero. Add this to preveint that.
$break_length=($break_length)? $break_length : 30;  // if a slash wasn't found, set an arbitrary length 
$newtext = wordwrap($url, $break_length, "<br />", true); 
echo " <br>wordwrap with break on slash break_length is <br>$newtext<br />"; 
/*RESULT
http://www.somedomain.com/Directory
/Another_Directory/program_name.php
*/
 
 
// of course you could get a VERY long url that exceeded your length, even with the break on the slash 
 $url="www.somedomain.com/Directory/Another_Directory/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcitement.php"; 
$break_length=($break_length)? $break_length : 30;  // if a slash wasn't found, set an arbitrary length 
$newtext = wordwrap($url, $break_length, "<br />", true); 
 
//you can then do an absolute wordwrap on the wordwrapped variable 
$newtext=wordwrap($newtext, 50,"\n"); 
echo " <br>wordwrap with break on slash with extra long program and forced break  is <br>$newtext<br />"; 
/*RESULT
www.somedomain.com/Directory/Another_Directory
/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
citement.php
  
?>

Open in new window

0
 
LVL 6

Expert Comment

by:birwin
ID: 24276249
On my last example, I missed adding the
$break_length=strpos($url,"/",30);
after the $url=
0
 
LVL 6

Expert Comment

by:birwin
ID: 24276281
I also forgot to close the last comment.
Add     */
Just above ?>
0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24283115
katlees:

Here is the solution.

=NerdsOfTech
EE :: PHP & DB Zone Master
while ( $row = mysql_fetch_assoc($result) ) {
 $facility_id = $row['ID'];
$Email = $row['Email'];
$Website = $row['Website'];
 
  echo("<span style="width: 150px; overflow: scroll;"><font class=\"style8\"><a href=\"http://$Website\" target=\"_blank\"><font color=\"blue\">$Website</font></a></span><br>");
  echo("<span style="width: 150px; overflow: scroll;"><font class=\"style8\"><a href=\"mailto:$Email\"><font color=\"blue\">$Email</font></a></span>");

Open in new window

0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24283137
If you want the display width changed: alter the area inside style="width:###px, where it is currently set to 150px (150 pixels), to a different size such as 200px or 100px, etc.

=NerdsOfTech
0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24283219
You could also truncate the URL (make it shorter) on the link display side and leave the href intact:

I made it cutoff a URL that is 25&+ characters long to 25 characters plus '...'. You can make the URL output longer or shorter by changing these two lines:

1.)
 if (strlen($str) >= 25){
2.)
  $sWebsite = substr($Website, 0, 25) . '...';

In my opinion this way looks better.

=NerdsOfTech
while ( $row = mysql_fetch_assoc($result) ) {
 $facility_id = $row['ID'];
 $Email = $row['Email'];
 $Website = $row['Website'];
 
 if (strlen($str) >= 25){
  $sWebsite = substr($Website, 0, 25) . '...';
 }else{
  $sWebsite = $Website;
 }
 
 echo("<font class=\"style8\"><a href=\"http://$Website\" target=\"_blank\" alt=\"http://$Website\"><font color=\"blue\">$sWebsite</font></a></span><br>");
 echo("<font class=\"style8\"><a href=\"mailto:$Email\"><font color=\"blue\">$Email</font></a></span>");

Open in new window

0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24284333
Thanks WhackAMod!
0
 

Author Comment

by:katlees
ID: 24290612
birwin - how do you use that with the code I have supplied. I don't care where the URL breaks.
NerdsOfTech - your first option works but I can't have the scroll bar on there. Your second option didn't work.
0
 
LVL 6

Accepted Solution

by:
birwin earned 2000 total points
ID: 24291058
If you don't care where it breaks, the code below will work. Change the 40 to whatever width you want.

while ( $row = mysql_fetch_assoc($result) ) {
 $facility_id = $row['ID'];
$Email = $row['Email'];
$Website = $row['Website'];
 
  echo("<font class=\"style8\"><a href=\"http://$Website\" target=\"_blank\"><font color=\"blue\">".wordwrap($Website, 40, "<br />", true)."</font></a><br>");
  echo("<font class=\"style8\"><a href=\"mailto:$Email\"><font color=\"blue\">".wordwrap($Email, 40, "<br />", true)."</font></a>");
 

Open in new window

0
 

Author Closing Comment

by:katlees
ID: 31576514
Perfect... Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month19 days, 3 hours left to enroll

834 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