473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Shortening URL's using PHP

I have a guestbook on one of my sites where the entires are formatted to be
x pixles in width. Now the text is input and output from a "blob" field
from a database... but when people input long URL's into their entries, the
formatting of the page is ruined.

Can anyone suggest a way for PHP to "read through" the text and substitute
a URL with a short text link, but still keeping the URL so it's clickable?
From my observations, the URL's may or may not (usually not) have the
prefix http://.

All I can think of is for PHP to count how many consecutive characters
there are before a "space" character, and substitute if it's over x
characters in length. But I don't know if this is possible.

Would prefer suggestions how to do it rather than actual code (I want some
challenge after all).

Thanks.

Dariusz
Jul 17 '05 #1
4 1503
On Tue, 22 Mar 2005 08:58:07 GMT, ng@lycaus.plusYOURSHIT.com (Dariusz) wrote:
Can anyone suggest a way for PHP to "read through" the text and substitute
a URL with a short text link, but still keeping the URL so it's clickable?


This doesn't address your consideration about shortening based on location of
spaces, but it shortens URLs that are more than 50 chars in length and appends
ellipses :

echo "<a href=\"" . $url . "\">" . substr($url, 0, 50);

if (strlen($url) > 50) {
echo "&nbsp;.&nbsp;.&nbsp;.";
}

echo "</a>";
HTH

Jul 17 '05 #2
Dariusz wrote:
I have a guestbook on one of my sites where the entires are formatted to be
x pixles in width. Now the text is input and output from a "blob" field
from a database... but when people input long URL's into their entries, the
formatting of the page is ruined.

Can anyone suggest a way for PHP to "read through" the text and substitute
a URL with a short text link, but still keeping the URL so it's clickable?
From my observations, the URL's may or may not (usually not) have the
prefix http://.

All I can think of is for PHP to count how many consecutive characters
there are before a "space" character, and substitute if it's over x
characters in length. But I don't know if this is possible.

Would prefer suggestions how to do it rather than actual code (I want some
challenge after all).

Thanks.

Dariusz


A perfect use for a regular expression.

-Joe
Jul 17 '05 #3
Dariusz wrote:
I have a guestbook on one of my sites where the entires are formatted
to be x pixles in width. Now the text is input and output from a
"blob" field from a database... but when people input long URL's into
their entries, the formatting of the page is ruined.

Can anyone suggest a way for PHP to "read through" the text and
substitute a URL with a short text link, but still keeping the URL so
it's clickable? From my observations, the URL's may or may not
(usually not) have the prefix http://.

All I can think of is for PHP to count how many consecutive characters
there are before a "space" character, and substitute if it's over x
characters in length. But I don't know if this is possible.

Would prefer suggestions how to do it rather than actual code (I want
some challenge after all).

Thanks.

Dariusz


Have a look at this:

http://www.php.net/manual/en/function.preg-replace.php

Read the comment by syd_egan. Now, I know you didn't want actual code, but
there are several challenges here, one of them is that syd_egan's code
presumes that either the http:// or ftp:// schemes are present, so there is
your first challenge. Good luck.
Jul 17 '05 #4
In article <1A*********************@news.xtra.co.nz>, "Nik Coughin" <nr***********@woosh.co.nz> wrote:
Have a look at this:

http://www.php.net/manual/en/function.preg-replace.php

Read the comment by syd_egan. Now, I know you didn't want actual code, but
there are several challenges here, one of them is that syd_egan's code
presumes that either the http:// or ftp:// schemes are present, so there is
your first challenge. Good luck.


Thank you to all who have replied to my problem with your pointers, I hope
I can work a solution myself ;-).

Dariusz
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Herb Kauhry | last post by:
Just got into PHP. Bought and read Core PHP Programming by Atkinson and Suraski. I have a question that didn't seem to be addressed there or anywhere else I've looked so far. So the answer is...
2
by: kaptain kernel | last post by:
how does one shorten an array based on a key value. Say I have this: array("Car1"=>"Porsche","Car2"=>"Merc","Car3"=>"Ford","Car4"=>"Toyota"); and I want to pop "Car2" out of the array,...
3
by: asdfkajsdflkjsadlfkjoewqifoeiwjf | last post by:
Hi I have a string: $string="aslkjdaslkdiwedweodij" When printing it out, I want it to be limited to 10 characters followed by "..." Result:
4
by: Kardon Coupé | last post by:
Dear All, I've been advised by a friend of mind that in VB you can shorten your source (he doesn't know how to do it, it said someone told him), for example I could have...
1
by: Robert Bravery | last post by:
HI all, I am using: using System.Text; using Microsoft.Office.Interop.Excel; But both have a datatable Whenever I want to initialise a variable of datatable, I have be more precise as to...
6
by: kberry | last post by:
I am clearing Textboxes on a form... this is loop I have came up with but was wondering if it can be shorter or not as long... Can anyone help? Dim controlOnForm As Control 'Places a control...
3
by: ViperBlade | last post by:
Hi, i'm unsure of the versions of MySQL and PHP I'm using (although my webhost 'guarantees' the latest), but I don't think the answer would vary that much between versions. Now to the point: I'm...
2
by: Jeigh | last post by:
Hello, I'm pretty sure I've seen this here before but I've tried searching (not sure on the keywords to search) and nothing came up so here's what I want to do I've just developed a news system...
3
by: vern | last post by:
Just curious, how would I shorten the following code? $sql_forum_posts_num = "SELECT forum_posts FROM ". $db_db.".phpbb_forums WHERE forum_id = '$forum_id'"; $forum_posts_num =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.