Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help with a PHP link parsing script
Old 09-18-2005, 03:41 PM Help with a PHP link parsing script
Novice Talker

Posts: 6
Trades: 0
Dear ones,

I really need a script for my blog, but I can't figure out how to make it.

In my blog I allow visitors to post comments. For security reasons,
I don't allow them to insert HTML tags. So when a user posts a comment,
that comment is processed by PHP so that "<" and ">" are converted to
HTML entities. Tags are displayed but do not work, since they're not parsed
by the browser as HTML tags.

But what if the user tries to insert a link in his/her comment by simply typing the address?

It will appear as this: htttp://www.example.com/index.php?parameter=etc

But I want it to appear as a clickable link: http://www.example.com/index.php?parameter=etc


So I wrote this function.

PHP Code:
function parse_links($text) {
        
$text ereg_replace('http([^\ <>]+)','<a href="http\\1">http\\1</a>',$text);
        return 
$text;

So the link is parsed and transformed into a clickable link <a href="...">...</a>

That's the point: if the user inserts a very, very long link,
the design of my blog will be destroyed.

Example: htttp://www.example.com/index.php?parameter=1&parameter=1&parameter=1&para meter=1&parameter=1&parameter=1&parameter=1&parame ter=1&parameter=1&parameter=1
So I do need a function that CUTS the link after the 30th character, something like this:
http://www.example.com/index.php?par...=1&parameter=1

Can you help me?

Thank you for your patience and attention.

Last edited by giacomogd1; 09-18-2005 at 05:18 PM..
giacomogd1 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-18-2005, 03:57 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Substring will do it:

PHP Code:
$link "http://www.example.com/index.php?this=is&a=very&long=link&indeed=so&very=long&that=it&causes=problems";

$htmlforlink="<a href=\"$link\">".substring($link,0,30) . "..." substring($link,-15)."</a>"

The first argument is the string to get the substring from, the next number isthe character to start at, and the last argument is optional and says how many characters to return. The code above will give you the first 30 characters, followed by ... followed by the last 15 characters.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 09-18-2005, 04:03 PM
Novice Talker

Posts: 6
Trades: 0
Quote:
Originally Posted by 0beron
Substring will do it:

PHP Code:
$link "http://www.example.com/index.php?this=is&a=very&long=link&indeed=so&very=long&that=it&causes=problems";

$htmlforlink="<a href=\"$link\">".substring($link,0,30) . "..." substring($link,-15)."</a>"

The first argument is the string to get the substring from, the next number isthe character to start at, and the last argument is optional and says how many characters to return. The code above will give you the first 30 characters, followed by ... followed by the last 15 characters.
No, it cannot work. The links are not isolated and put into variables. There is no $link variable.

Links are mixed with the rest of the comment itself, and have to be recognized and cut. Please, read my post another time and you'll understand. Thank you.
giacomogd1 is offline
Reply With Quote
View Public Profile
 
Old 09-18-2005, 04:37 PM
Republikin's Avatar
Defies a Status

Posts: 3,189
Trades: 3
Oberon gave you a great example that will work for your situation. I suggest you read his post again and be a little more greatful.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Republikin is offline
Reply With Quote
View Public Profile
 
Old 09-18-2005, 04:43 PM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
There is no $link variable.
There will be after you have written it into the section of code that does the auto linking
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-18-2005, 05:08 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Quote:
Originally Posted by giacomogd1
So the link is parsed and transformed into a clickable link <a href="...">...</a>
If you have code to do this, you can use the substring method, or should be able to with very little effort.

Have you got this part working or do you need a hand with that too? Is it just the link length you want fixing or the parsing too?
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 09-18-2005, 05:13 PM
Novice Talker

Posts: 6
Trades: 0
I'm sorry I've been not kind. It's a matter of language, maybe.

I can't figure out how I can apply the solution Oberon provided.

This is the script (in a simplified form):

PHP Code:
$text $_GET['text'];
$user $_GET['user'];

/* TEXT PROCESSING BEGINS */
$text ereg_replace('http([^\ <>]+)','<a href="http\\1">http\\1</a>',$text);
/* TEXT PROCESSING ENDS */

mysql_query("INSERT INTO table (user, text) VALUES ('".$user."', '".$text."')"
Where should I put Oberon's code in order to shorten the link?
giacomogd1 is offline
Reply With Quote
View Public Profile
 
Old 09-18-2005, 05:16 PM
Novice Talker

Posts: 6
Trades: 0
Quote:
Originally Posted by 0beron
If you have code to do this, you can use the substring method, or should be able to with very little effort.

Have you got this part working or do you need a hand with that too? Is it just the link length you want fixing or the parsing too?
The problem is to combine "link parsing" and "link shortening" processes together.
All the links have to be identified (and I already wrote the code to do this), and
their anchor has to be cut in order no to be too long (that's the code you wrote me).
Now let's do the two thing at the same time.

Thank you for your help.
giacomogd1 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Help with a PHP link parsing script
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.29403 seconds with 12 queries