The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
String recognition and substitution
Discuss String recognition and substitution in the PHP Development forum on Dev Shed. String recognition and substitution PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 25th, 2000, 10:34 AM
|
|
Contributing User
|
|
Join Date: Feb 2000
Location: Aalborg, Denmark
Posts: 36
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
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
|

February 25th, 2000, 10:42 AM
|
|
Contributing User
|
|
Join Date: Feb 2000
Location: Aalborg, Denmark
Posts: 36
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
HEh.. I can devshed uses the very same feature in this bbs
------------------
// Martin
|

February 25th, 2000, 03:33 PM
|
|
Contributing User
|
|
Join Date: Feb 2000
Location: Englewood, CO
Posts: 30
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
|

February 29th, 2000, 02:54 AM
|
|
Contributing User
|
|
Join Date: Feb 2000
Location: Aalborg, Denmark
Posts: 36
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
|
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
|

February 29th, 2000, 05:48 AM
|
|
Contributing User
|
|
Join Date: Oct 1999
Location: Helden,Limburg,The Netherlands
Posts: 33
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)
|

February 29th, 2000, 07:22 AM
|
|
Contributing User
|
|
Join Date: Feb 2000
Location: Aalborg, Denmark
Posts: 36
Time spent in forums: < 1 sec
Reputation Power: 14
|
|
|
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®ards
// Martin
|

February 29th, 2000, 06:12 PM
|
|
Junior Member
|
|
Join Date: Feb 2000
Location: Aurora
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
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).]
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|