Results 1 to 8 of 8

Thread: Removing unnecessary dots in a URL with preg_replace

  1. #1
    Join Date
    Oct 2012
    Posts
    155
    Thanks
    21
    Thanked 1 Time in 1 Post

    Default Removing unnecessary dots in a URL with preg_replace

    Let's say somebody post URLs that look like this:


    http://something.com.
    http://something.org.
    http://something.info.

    or like this:

    http://something.com...
    http://something.org...
    http://something.info...

    or like this:

    ...http://something.com
    ...http://something.org
    ...http://something.info

    Is there a way to clean up URLs from any dots that are not supposed to be there?

    Any input will be appreciated.
    Last edited by djr33; 10-05-2013 at 04:51 AM. Reason: don't autoparse links

  2. #2
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    375
    Thanks
    5
    Thanked 35 Times in 33 Posts

    Default

    php code:
    PHP Code:
    trim('...http://example.com...','.'); 
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  3. #3
    Join Date
    Oct 2012
    Posts
    155
    Thanks
    21
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by Crazykld69 View Post
    php code:
    PHP Code:
    trim('...http://example.com...','.'); 
    But how do you translate this to ALL URLs?

    I get the http://example.com part, but I want this to work for any URL. Is there a way?

  4. #4
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    375
    Thanks
    5
    Thanked 35 Times in 33 Posts

    Default

    It would depend. What code are you using to locate which string is a URL?
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Separate the problem into logical steps, and ask about one at a time.

    Steps (I'm guessing):
    1. Get input.
    2. Locate URLs in input.
    3. With an individual URL, remove the extra content.
    4. Store/display/send output.


    The post above solves (3). What are your plans for (2)? Have you already dealt with (1) and (4)? Please give us the information we'd need to help you.


    Additionally, remember that anything, even this relatively basic task, involving the processing of natural language text can be very difficult. You can approximate good results, but it may never be perfect. Your desires are phrased vaguely, which will only make the problem worse. If you simply want to remove the dots, that's much easier than removing any potential other text that might be there. (What about colons, commas, etc.? And parentheses?) Often a real problem of Step (3) above will be predicting what you want to do in terms of how you'd edit the text.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Oct 2012
    Posts
    155
    Thanks
    21
    Thanked 1 Time in 1 Post

    Default

    I got it figured out. I found something online (spent 2 hours this morning looking for it).


    Sorry I couldn't explain better what I needed when I started the thread.
    Last edited by qwikad.com; 10-05-2013 at 07:02 PM. Reason: Found a solution... I think.

  7. #7
    Join Date
    Mar 2011
    Location
    N 11░ 19' 0.0012 E 142░ 15' 0
    Posts
    1,524
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    If you've found a solution, please feel free to share it so other people with the same question can read about it and find the answer!
    Last edited by keyboard; 10-05-2013 at 11:40 PM.

  8. #8
    Join Date
    Oct 2012
    Posts
    155
    Thanks
    21
    Thanked 1 Time in 1 Post

    Default

    Here's what I am using:


    Code:
    $text = preg_replace( '/(\b)(http|ftp|https|mailto)([\:\/\/])([A-z0-9~!@$%&*()_+:?,.\/;=#-]{2,}[A-z0-9~@$%&*_+\/=])/', " $2$3$4 ", $text);

    I decided to put a space on each end of a link, instead of trying to strip all/any characters that may come in contact with it (which in my case used to interfere with a link's accuracy).

    It's not perfect, it won't work with domain names ending in .com.de or .org.uk, etc. but other than that I am pretty happy with it.


    Thanks!
    Last edited by qwikad.com; 10-07-2013 at 12:02 AM.

  9. The Following User Says Thank You to qwikad.com For This Useful Post:

    keyboard (10-07-2013)

Similar Threads

  1. Replies: 15
    Last Post: 05-26-2013, 09:34 PM
  2. Unnecessary spaces in accordion menu
    By userit in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 02-23-2011, 03:17 PM
  3. Unnecessary PHP? Alternatives?
    By bistromath in forum Looking for such a script or service
    Replies: 7
    Last Post: 08-18-2008, 01:55 PM
  4. Replies: 2
    Last Post: 06-09-2008, 12:42 PM
  5. Ultimate Fade in - unnecessary space under it
    By JimHo in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 02-02-2007, 03:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •