Page 3 of 3 FirstFirst 123
Results 21 to 25 of 25

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

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

    Default

    Actually, you are incorrect. Not sure why you have to be so stuck up but anyways I'll explain it to you.
    I think the main point of this is for him to find in a string all the websites that are actually websites. You are using preg_replace(); however, there is more than one way of doing something. If you actually view your code it doesnt fully wrap the code. On another note if you add this to the end of string "team.error", well it looks like we have a new type of website.

    What my code does is actually checks to see if the website is REAL and FULLY wraps the code in html. If he doesnt want the html and just the string well that's easy to do.
    Why dont you look and test the code before stating:
    Quote Originally Posted by traq View Post
    @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.
    I find that uttly rude.
    Because the only reason i am here is to help people out and contribute.

    Not sure how my code doesnt help him out but it
    1. get the string and checks if there can be anything in there that contains websites
    2. if it is an actual website it FULLY and CORRECTLY wraps the string in html
    3. Doesn't mark fake websites as websites.

    Edited it a little:
    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 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.error';
    $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();
         
    $www preg_match('/www./',$test)?'':'www.'.$test;

      
    curl_setopt($chCURLOPT_URL$www);
      
    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.' ';  
      }

    thanks
    -DW
    Last edited by Deadweight; 09-14-2013 at 08:57 AM.

  2. #22
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Thank you for the code explanation Crazykid69 - I believe that is all traq was asking for. He's a really helpful chap and wouldn't have meant anything to come across as stuck-up or rude - try not to read it that way (I didn't).

    But in future, it would be great if you could provide the details as part of an all-inclusive answer because you may have interpreted something differently from the OPs question that might not have been immediately obvious to other contributors, or indeed the OP, if their information was a bit ambiguous.

    I think its good that you're taking time to help (there should be more helpful folks in life, dont you think?). We regular posters are always here to contribute - part of that is asking for clarification at times, either from the OP or from other contributors - its the best way to work towards an appropriate/comprehensive answer in this diverse community with so many ideas, backgrounds and strengths doing the rounds.
    Focus on Function Web Design
    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

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

    Default

    @Crazykld69, I think I phrased my post badly. I was concerned because I wasn't sure what you were trying to offer - thank you for your explanation. I understand wanting to be helpful - I appreciate it. I certainly didn't mean to be rude.

    To answer your points,

    ... I'm not sure what you mean by my code "not fully wrapping." preg_match does operate on the entire string.

    ... I did try out your code before asking for clarification. One of the OP's concerns was matching URLs with or without "http://" and/or "www." I agree; you're right that this will sometimes lead to false positives.
    Last edited by james438; 09-15-2013 at 03:49 AM. Reason: typo ;)

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

    Default

    @traq check your anchor wrap in this "SoCal.trevobuilder.com"

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

    Default

    ahh, I see what you're talking about. That happened for two reasons:

    First, as I mentioned in this post, I modified the regex I was using after my initial response. The changes I made were specifically to allow matching URLs with multiple subdomains and subdomains other than "www". When I edited my post to show those changes, I changed the regex itself as well as my commentary on it. However, I overlooked the regex in the example I gave.

    Second, when I posted the regex, I forgot to use [noparse] tags, so VBulletin thought I was posting a hyperlink and added [url] tags to parts of the regex (in a discussion about parsing URLs, the irony does not escape me). As you can see, the regex doesn't work properly with those tags inserted. What it comes down to is I tested the code before posting it, but it didn't occur to me to test it after posting.

    Here's the regex, without the extraneous BBCode tags:

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

    And here's the correct version of the test:
    PHP Code:
    <?php

    $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 );
    and the results:
    Code:
    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 <a href="http://www.SoCal.trevobuilder.com">www.SoCal.trevobuilder.com</a> 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 <a href="http://trevocorporate.com/coach/sjahr">trevocorporate.com/coach/sjahr</a> to register on the Presidential Elite team.
    thanks for pointing that out.

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
  •