PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 25th, 2000, 10:34 AM
kaasgaard kaasgaard is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: Aalborg, Denmark
Posts: 36 kaasgaard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 14
Send a message via ICQ to kaasgaard
Hi

I'm currently doing a site in PHP with MySQL as DB. I have a form with a textfield and I'm searching for a method to recognize "simple" URL's (eg. www.devshed.com) and store them in my DB as <a href="simple-url">simple-url</a>.

The user who entered the simple url can edit his/her post so i also need a way to translate it back.

The user can't write any html tags in the text (they get stripped) but i would like substitution to take place as a nice little feature.

I've tried to do it with ereg_replace() but without any luck.

I hope I have made myself clear



------------------
// Martin

Reply With Quote
  #2  
Old February 25th, 2000, 10:42 AM
kaasgaard kaasgaard is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: Aalborg, Denmark
Posts: 36 kaasgaard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 14
Send a message via ICQ to kaasgaard
HEh.. I can devshed uses the very same feature in this bbs

------------------
// Martin

Reply With Quote
  #3  
Old February 25th, 2000, 03:33 PM
TroutMask TroutMask is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: Englewood, CO
Posts: 30 TroutMask User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 14
I wouldn't store the a href=, etc.

Since it's always going to be that, just store the actual www.hotlinecentral.com URL and add on the a href= whenever you print it out. You're storing lots more data than you need to and you're forced to parse out the small URL every time. If you just stored the www.hotlinezone.com URL ;-), you'd save space and trouble.

imho,

-TM


Reply With Quote
  #4  
Old February 29th, 2000, 02:54 AM
kaasgaard kaasgaard is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: Aalborg, Denmark
Posts: 36 kaasgaard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 14
Send a message via ICQ to kaasgaard
That is a good idea!

I still need a way to recognize the url when the user submits it.

Do you (or others) have an idea of how to do it? I've tried to locate the url with different regular patterns, but i'm relatively new to reg. expressions so any ideas are welcome.



------------------
// Martin

Reply With Quote
  #5  
Old February 29th, 2000, 05:48 AM
Ramon Ramon is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 1999
Location: Helden,Limburg,The Netherlands
Posts: 33 Ramon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 14
At the moment I do not time to give the exact code but I can tell you the principle way to check for a simple URL. You should check first if there is this form:

string.string.string
(e.g. www.amazon.com)

or

string.string.string.string
(e.g. www.amazon.co.uk)

Then you can check (although there are a lot of exceptions) if the first string before the first dot is "WWW" But this method would leave out URL's such as webdesign.boradoli.nl so it might not be a good idea.

The last thing you can check is whether the last (or the last two, in the second example) string consists of the most common endings like com, net, org, edu, co.uk, de, nl, no, etc...The longer you make this list, the better.

In general the more sure you want that a string is actually an URL the more restrictive you are on the type of URL allowed.

One alternative method could be to let PHP check if it can find index.htm, index.html, default.htm or default.html on the given URL, with a file exists function. This might take a bit more time to process, but it is a much easier and less restrictive alternative.

------------------
Ramon Litjens
Boradoli Web Design
(www.boradoli.nl)

Reply With Quote
  #6  
Old February 29th, 2000, 07:22 AM
kaasgaard kaasgaard is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: Aalborg, Denmark
Posts: 36 kaasgaard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 14
Send a message via ICQ to kaasgaard
Again a good idea. Thanks.

Your method is what i'm currently trying to use (without the proper regex, tho).

The protocol I imagine to use is:
1) check for [[:alnum:]]{1,}.[[:alnum:]] where the last .[[:alnum:]] can be repeated n number of times (perl style). It should also check for "/something" repeated the same way and finaly the endings "html, phtml, php, etc.) in order to catch the full url and not restrict it in any ways.
2) if any match store it in a var
3) check for more occurences in the same string and store them (repeat until no more matches)
4) do the actual replacement (eg using str_replace)
5) parse the string to the browser.

The thing i need is any idea to write the regular search pattern (a bugger i know)

thanx&regards

// Martin

Reply With Quote
  #7  
Old February 29th, 2000, 06:12 PM
FooKilla FooKilla is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: Aurora
Posts: 1 FooKilla User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to FooKilla
Hey, I had this same problem...so I wrote my own function for it.

I've been meaning to fix it up but it works good enough for now.

Here is a quick idea of how I did it. I used 2 offset vars. $offset2 to hold the current position in the string, and $offset1 to hold the first occurance of 'http://' after $offset2.

I then add the html code around the http://whatever.com string I found and store it in $url.

Then I do

$string = substr(0 to $offset1) . $url . substr($offset2 to end);

Then I update the offsets accordingly.

This will turn all http://urls.com into <a href type links.

Here is the code, hopefully it makes sense.

/*===============================================================================================
| N W _ H T T P 2 H R E F [c](01-07-00) [m](01-15-00) SML |
| |
|-returns $str with any http:// strings changed to <a href...> |
===============================================================================================*/

function nw_Http2Href($str)
{
$offset1 = 0;
$offset2 = 0;
$str_length = strlen($str);

while($offset2 < $str_length)
{
// find http:// starting at char $offset2
$offset1 = strpos($str , "http://" , $offset2);

if(!is_int($offset1)) break;

$offset2 = $offset1 + 7;

/*get url, look for space or < or new line, this isnt perfect but how many urls have spaces? not many... this could use some fixing up though.*/

while($offset2 < $str_length && $str[$offset2] != ' ' && $str[$offset2] != '<' && $str[$offset2] != 'n')
{
$offset2++;
}

// get url
$url = substr($str , $offset1 , $offset2 - $offset1);

// make url
$url = "<A HREF="" . $url . "" TARGET="_new">" . $url . "</A>";

// re - combine string
$str = substr($str , 0 , $offset1) . $url . substr($str , $offset2);

// 29 is the # of chars added for the link

$str_length += 29 + ($offset2 - $offset1);
$offset2 += 29 + ($offset2 - $offset1);
}

return $str;
}




[This message has been edited by FooKilla (edited February 29, 2000).]

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > String recognition and substitution

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap