Page 1 of 3 123 LastLast
Results 1 to 10 of 25

Thread: PHP preg_replace adding http:// only when missing

  1. #1
    Join Date
    Oct 2012
    Posts
    150
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default PHP preg_replace adding http:// only when missing

    Often people post links in our classifieds that start just with just www. or nothing at all (for instance www.blahblah.com or blahblah.com).

    I need a preg_replace function that will add http:// to any link that doesn't have it.

    So if a link is: www.blahblah.com it should become http://www.blahblah.com
    If a links is blahblah.com it should become http://blahblah.com

    This is the preg_replace line that needs to be filled: $text = preg_replace( '//', "", $text);

    Thank you for any suggestions.
    Last edited by qwikad.com; 09-12-2013 at 06:49 PM.

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    1,946
    Thanks
    16
    Thanked 304 Times in 303 Posts
    Blog Entries
    11

    Default

    Have you tried anything from Google? This came up first for me and looks like it does what you want: http://stackoverflow.com/questions/1...ce-help-in-php
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  3. #3
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    342
    Thanks
    5
    Thanked 33 Times in 31 Posts

    Default

    Are you wanting something like this:
    PHP Code:
    <?php

    $string 
    'blahblah.com';
    $add '';

    $add .= strpos($string,'http://') !== false '' 'http://';
    $add .= strpos($string,'www.') !== false '' 'www.';

    $add .= $string;

    echo 
    $add;

    ?>
    Oh since you dont want to add www. delete
    PHP Code:
    $add .= strpos($string,'www.') !== false '' 'www.'
    OOps i should have read the whole thing. You need to use a specific tag xD
    Last edited by Deadweight; 09-12-2013 at 08:26 PM. Reason: Forgot how to read

  4. #4
    Join Date
    Oct 2012
    Posts
    150
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default

    I searched for 20-30 minutes before I posted that question here. I don't know how you're able to find the right solution so quickly.

    Can you verify that this is a viable solution:

    $text = preg_replace( '/^(?:http:\/\/)?(.*)/', "http://$1", $text);


    Do you think it's going to add http:// to any domain name that doesn't have it?

    What about a domain name that already has a http://? Is it still going to work? On that page: http://stackoverflow.com/questions/1...ce-help-in-php it's not clear if the guy who posted the solution has solved that issue.


    Thanks.

  5. #5
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    342
    Thanks
    5
    Thanked 33 Times in 31 Posts

    Default

    PHP Code:
    $string 'blahblah.com';
    if(
    preg_match('/http:\/\//',$string)){
    echo 
    'found';
    }else{
    echo 
    'not found';    

    I have a reading problem today..... You need replace xD

    Also it does work with the solution above... Do you have to use replace or may you use anything?

    Good free place to test my php if you like:
    http://www.compileonline.com/execute_php_online.php
    Last edited by Deadweight; 09-12-2013 at 08:51 PM.

  6. #6
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    1,946
    Thanks
    16
    Thanked 304 Times in 303 Posts
    Blog Entries
    11

    Default

    I haven't tested anything (currently on iPhone) but you could feed in a few sample variables on a test page and see what pops out the other side? That's all I'd do.
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by qwikad.com View Post
    Can you verify that this is a viable solution:
    Code:
    $text = preg_replace( '/^(?:http:\/\/)?(.*)/', "http://$1", $text);
    It depends on your problem. This will take any string (which may or may not have "http://" at the beginning) and make sure it has "http://" at the beginning. So, for example, "blahblah.com" will become "http://blahblah.com".

    However, "Please visit blahblah.com today!" will become "http://Please visit blahblah.com today!".

    So, have you already captured the domains as their own strings? If so, yes, this will work fine. If not, then no.

    URLs are typically identified by "http://" and/or "www." at the beginning. It gets very difficult to reliably identify them without one of those.

  8. #8
    Join Date
    Oct 2012
    Posts
    150
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by traq View Post
    It depends on your problem. This will take any string (which may or may not have "http://" at the beginning) and make sure it has "http://" at the beginning. So, for example, "blahblah.com" will become "http://blahblah.com".

    However, "Please visit blahblah.com today!" will become "http://Please visit blahblah.com today!".

    So, have you already captured the domains as their own strings? If so, yes, this will work fine. If not, then no.

    URLs are typically identified by "http://" and/or "www." at the beginning. It gets very difficult to reliably identify them without one of those.


    Funny, I just tested it and it does exactly what you've said.

    Ok, can you suggest a partial solution to this then? Forget about blahblah.com. Let's say I only want to target URLs that have www. in them. It will take care of 95% of the cases.

    Can you suggest something workable that will add http:// to URLs that have www. but are missing http:// and if URLs already have http:// they should not be changed in any shape or form?

    Thank you.

  9. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    So, the URLs are not yet separated from the rest of the text, is that correct?

    I will figure something out when I get home today. In the meantime, you might look through Google (or stackoverflow) for "PCRE" + "URL".

  10. #10
    Join Date
    Oct 2012
    Posts
    150
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by traq View Post
    So, the URLs are not yet separated from the rest of the text, is that correct?

    I will figure something out when I get home today. In the meantime, you might look through Google (or stackoverflow) for "PCRE" + "URL".
    Right. Just picture a classified ad. Lots of text and somewhere in the middle there's a URL. Like in this example:

    http://qwikad.com/0/posts/12-busines...ith-Trevo.html

    Looking forward to checking out your solution. Will google what you've suggested.

Similar Threads

  1. Resolved preg_replace
    By ggalan in forum PHP
    Replies: 2
    Last Post: 02-25-2012, 04:10 AM
  2. preg_replace
    By neo_philiac in forum PHP
    Replies: 0
    Last Post: 12-22-2008, 03:36 PM
  3. Replies: 2
    Last Post: 09-09-2008, 03:01 PM
  4. Can't use brackets in preg_replace
    By rctxtreme in forum PHP
    Replies: 1
    Last Post: 08-30-2007, 11:06 PM
  5. PCRE and preg_replace
    By james438 in forum PHP
    Replies: 2
    Last Post: 07-11-2007, 08:43 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
  •