Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 25

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

  1. #11
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Try this - #\b(?:[url]http://)?(www\.)?(([/url][a-z0-9_-]{2,}\.)+[a-z]{2,}(/[\w\+\-\?\&\;]*)*)\b#i

    Code:
    #                         delimiter (start of pattern)
      \b                      word boundary
        (?:http://)?          optional, non-capturing "http://"
        (www\.)?              optional, capturing (will be $1) "www."
        (                     start of capturing group (will be $2)
          (                     start of sub-pattern (will be $3)
            [a-z0-9_-]{2,}        something that looks like a domain name
            \.                    followed by a dot
          )+                    end of $3 - one or more matches
          [a-z]{2,}             followed by something that looks like a TLD
          (                     start of sub-pattern (will be $4)
            /                     a slash
            [\w\+\-\?\&\;]*       followed by something that looks like a path and/or query string
          )*                    end of $4 - zero or more matches
        )                     end of $2 - one match only ($2 also contains $3 and $4)
      \b                      word boundary
    #i                        delimiter (end of pattern), case-insensitive
    tested:
    PHP Code:
    $text "Looking for marketers who want to work. Ready to change your life? Trevo offers one product - an all natural, vegan, kosher nutritional supplement with 174 nutraceuticals. Visit www.SoCal.trevobuilder.com for product info & to purchase. Find me on facebook at Trevo SoCal or @TrevoSoCal on twitter. Low start up, cost covers first 3 bottles or larger packages available. No registration fees. Start making money this week! Visit trevocorporate.com/coach/sjahr to register on the Presidential Elite team.";

    $regexp "#\b(?:http://)?(www\.)?([a-z0-9_-]{2,}\.[a-z]{2,}(/[\w\+\-\?\&\;]*)*)\b#i";

    $hypertext preg_replace$regexp,'<a href="http://$1$2">$1$2</a>',$text );

    print 
    htmlentities$hypertext ); 
    Note that this pattern will match most (not all) valid URLs, and will not match most (not all) non-URL text. It might be a good, workable compromise for your purpose.

    Edit: I changed this after posting it. Compare and make sure you're using this version.
    Last edited by traq; 09-13-2013 at 01:58 AM. Reason: fixed [icode] tags

  2. #12
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Why do you want to add the http:// to each url if it is not there? Are you looking to hyperlink the urls or do you have a script that already does that? I am also interested, because I like to collect pcre patterns/scripts that relate to manipulating urls.

    EDIT: Actually, I notice that traq's script does just that, but I do not believe that was specifically asked for, but it may be what you were looking for. I have one that does what traq posted, but mine specifically avoids hyperlinking urls that are missing the www prefix.
    Last edited by james438; 09-13-2013 at 02:32 AM.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #13
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by james438 View Post
    I notice that traq's script does just that [turns urls into hyperlinks], but I do not believe that was specifically asked for...
    Whup, you're right. I wrote that without re-reading the thread. If you don't want the URLs hyperlinked, just replace
    PHP Code:
    preg_replace$regexp,'<a href="http://$1$2">$1$2</a>',$text ); 
    with
    PHP Code:
    preg_replace$regexp,'http://$1$2',$text ); 

  4. #14
    Join Date
    Oct 2012
    Posts
    180
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default

    traq I realized that was an issue and changed it to

    Code:
    preg_replace( $regexp,'http://$1$2',$text );
    Still it's not doing what I want it to do. You see, I've made a lot (and I mean a lot) of changes to my markdown over the months and I am wondering if something else is now interfering with your code. The way the URLs looked was something like this:

    http://www.someurl.comwww.someurl.com

    ...and despite the fact they had http:// in them, they stopped being clickable (all URLs in the entire website stopped being clickable).

    I wonder if the "print htmlentities" had something to do with that.

    I also wonder if a one liner (like the one I mentioned before) if modified can solve the issue. Is there a way to tell this line to add http:// only if www. is present in a URL and ignore a URL altogether if it already has http:// ? (sorry for being redundant).

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

    Thanks!

  5. #15
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by qwikad.com View Post
    ...and despite the fact they had http:// in them, they stopped being clickable (all URLs in the entire website stopped being clickable).
    I wonder if the "print htmlentities" had something to do with that.
    I used htmlentities for the test so you could see the HTML output. Simply do print instead.


    Quote Originally Posted by qwikad.com View Post
    ...and despite the fact they had http:// in them, they stopped being clickable (all URLs in the entire website stopped being clickable).
    the text "http://" does not create hyperlinks. If you removed the anchor markup from the replacement pattern (i.e., you replaced '<a href="http://$1$2">$1$2</a>' with 'http://$1$2'), then I would not expect the URLs to be hyperlinked.

    You hadn't mentioned markdown earlier. Are you using a markdown parser? At what point?


    Quote Originally Posted by qwikad.com View Post
    I also wonder if a one liner (like the one I mentioned before) if modified can solve the issue. Is there a way to tell this line to add http:// only if www. is present in a URL and ignore a URL altogether if it already has http:// ? (sorry for being redundant).
    I would hesitate to say until I know more about what you are actually doing - the above makes it clear that we do not have all the necessary information.

  6. #16
    Join Date
    Oct 2012
    Posts
    180
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default

    traq, I spent sometime browsing the web and I came upon this little function:

    Code:
    <?php
    /*** example usage ***/
    $string='http://www.phpro.org';
    echo makelink($string);
    
    /**
    *
    * Function to make URLs into links
    *
    * @param string The url string
    *
    * @return string
    *
    **/
    function makeLink($string){
    
    /*** make sure there is an http:// on all URLs ***/
    $string = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2",$string);
    /*** make all URLs links ***/
    $string = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</A>",$string);
    /*** make all emails hot links ***/
    $string = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i","<A HREF=\"mailto:$1\">$1</A>",$string);
    
    return $string;
    }
    
    ?>

    If I take that first line and re-do it to fit my markdown, do you think it may do what I want it to?


    Code:
    $text = preg_replace('/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i', "$1http://$2", $text);
    Is it going to add http:// to all links that have www and skip those that already have http://? I'd appreciate if you test it for me. I can't test it live on our classifieds until late at night when I don't have so many people posting ads. Don't want to make their links go all berserk if something is wrong with the code, even temporarily.


    Thank you.
    Last edited by qwikad.com; 09-13-2013 at 02:31 PM.

  7. #17
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    PHP Code:
    $string 'Looking for marketers who want to work. Ready to change your life? Trevo offers one product - an all natural, vegan, kosher nutritional supplement with 174 nutraceuticals. Visit www.SoCal.trevobuilder.com for product info & to purchase. Find me on facebook at Trevo SoCal or @TrevoSoCal on twitter. Low start up, cost covers first 3 bottles or larger packages available. No registration fees. Start making money this week! Visit trevocorporate.com/coach/sjahr to register on the Presidential Elite team. Welcome to facebook.com.';
    $new_string '';
    $string_explode explode(' ',$string);
      
    foreach(
    $string_explode as $key){
        
    $check strpos($key,'.') !== false?weblink($key):$key.' ';
        echo 
    $check;
    }
      
    function 
    weblink($test){
        
    $ch curl_init();

      
    curl_setopt($chCURLOPT_URL$test);
      
    curl_setopt($chCURLOPT_HEADERtrue);
      
    curl_setopt($chCURLOPT_NOBODYtrue);
      
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
      
    curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
      
    curl_setopt($chCURLOPT_MAXREDIRS10);

      
    $data curl_exec($ch); 
      
    $info curl_getinfo($ch); 
      
    $code $info['http_code']; 

      
    curl_close($ch);
      
      
    $good_array = array(100,101,200,201,202,203,206,300,301,302,304);
      
      if(
    in_array($code,$good_array)){
         
    $add preg_match('/http:\/\//',$test)?'':'http://';
        return 
    '<a href="'.$add.$test.'">'.$test.'</a> ';
      }else{
        return 
    $test.' ' ;  
      }

    FYI i added something to test if it does work. At the very end i added "Welcome to facebook.com." To test if the period would mess up my code and it doesnt. However, if it is ? ! or something weird then the website will not exists. I will have to fix that. - If you want me to fix this problem let me know. Its easy.

    Another note: "trevocorporate.com/coach/sjahr" doesnt work so it will not place a link around the website.

  8. #18
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by qwikad.com View Post
    traq, I spent sometime browsing the web and I came upon this little function:

    [ . . . code . . . ]

    If I take that first line and re-do it to fit my markdown, do you think it may do what I want it to?
    TBH, that seems to attempt the same task as my code snippet, but doesn't do as well. It won't catch URLs without "www", for example, nor URLs with a path component.

    As for your "markdown," I don't know, because you have not explained how/ when/ what you are doing with markdown (beyond the fact that you're using it).


    Quote Originally Posted by qwikad.com View Post
    I'd appreciate if you test it for me. I can't test it live on our classifieds until late at night when I don't have so many people posting ads. Don't want to make their links go all berserk if something is wrong with the code, even temporarily.
    I thought you might be a "cowboy coder." It would be well worth your while to do your dev/ testing locally, instead of on you live site. It is much less stressful, and much easier to recover from mistakes. It is not difficult at all to set up Apache + PHP on your local computer.


    Quote Originally Posted by Crazykld69 View Post
    [ ... ]
    FYI i added something to test if it does work. At the very end i added "Welcome to facebook.com." To test if the period would mess up my code and it doesnt. However, if it is ? ! or something weird then the website will not exists. I will have to fix that. - If you want me to fix this problem let me know. Its easy.

    Another note: "trevocorporate.com/coach/sjahr" doesnt work so it will not place a link around the website.
    @Crazykld69, I am not sure what you are trying to contribute with this post. The code you offered doesn't seem to address any of the OP's questions.

    If you do have something to contribute, please edit your post to more clearly explain.
    Last edited by traq; 09-14-2013 at 02:14 AM.

  9. #19
    Join Date
    Oct 2012
    Posts
    180
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default

    treq, I just tested it. The code is doing exactly what I want it to do. I will still need to post a bunch of ads to ensure it works in all possible situations, but as of now it seems to be working perfectly. It's this one:

    Code:
    $text = preg_replace('/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i', "$1http://$2", $text);
    In all honesty, I tried to implement the function you suggested, it wouldn't work. That's why I was looking for a one-liner to at least help the URLs that start with www. I am pretty sure if you had access to my files you would make it work, but unfortunately you are limited by my weak attempts to explain what I want to accomplish. But thank you for nudging me in the right direction nonetheless.
    Last edited by qwikad.com; 09-14-2013 at 05:00 AM.

  10. #20
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    You can use my regex as a "one-liner" - no problem. I didn't realize that's why you were still looking for another answer. (You just want "http://" added, correct? no HTML added?)

    PHP Code:
    $text preg_replace
        
    "#\b(?:http://)?(www\.)?([a-z0-9_-]{2,}\.[a-z]{2,}(/[\w\+\-\?\&\;]*)*)\b#i"
       
    ,"http://$1$2"
       
    ,$text 
    ); 
    If you're still having trouble integrating it with markdown, you'll need to explain ho you're using it.

    In any case, glad to have helped.

Similar Threads

  1. Resolved preg_replace
    By ggalan in forum PHP
    Replies: 2
    Last Post: 02-25-2012, 05:10 AM
  2. preg_replace
    By neo_philiac in forum PHP
    Replies: 0
    Last Post: 12-22-2008, 04: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
  •