Preg Replace Problem

Discussion in 'PHP' started by scottlpool2003, Feb 28, 2014.

  1. #1
    Hi, I'm having a problem changing a text URL into a hyperlink in a given string.

    It works O.K, but messes up a little dependeing on the string. E.g. it could be www.example.com. note the .

    It worked for most as they didn't have the . in but this particular one is: www.example.com/page.<br />

    I'm just a little unsure how to handle it as I can't remove the . in that instance because of .php. .html pages etc.

    Here is what I have so far:

    
    function addLinks($Text){
        if (preg_match("/http/", "$Text") OR preg_match("/www/", "$Text"))
        {
        $Text = str_replace(" www" , " http://www" , $Text);
        $Explode = explode(" ", $Text);
        foreach($Explode as $Check) {if (preg_match("/http/", "$Check") OR preg_match("/www/", "$Check")) {$Text = str_replace($Check , "$Check" , $Text);}}
        }
        return $Text;
    }
    
    PHP:
    Found here: http://css-tricks.com/snippets/php/find-urls-in-text-make-links/#comment-133881

    Any ideas on how to handle this?

    Thanks
     
    scottlpool2003, Feb 28, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    I'm not really sure why you have to get rid of that trailing period? It has no impact on web-addresses (normally)?
    Besides, that function looks a bit... clunky.
    I'm assuming this is to add a simple http: before whatever link you have in your text which doesn't have this? And thereafter I'm assuming there's some other function which takes this http:-link and actually creates a link from it? (Like a built-in text-parser?)
     
    PoPSiCLe, Feb 28, 2014 IP
  3. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #3
    Sorry I sent the wrong snippet. I have multiple people uploading news stories, they're copy and pasted in from edits and there's not enough time for them to add hyperlinks, so I thought about putting a function in there to handle them. Problem is, they're not consistent. Some will have http:// others simply have www. or have a full stop after them. Here's an example:

    But in the HTML it looks like:
    
    Apprenticeship vacancies can be found on the website www.example.co.uk/services/current-vacancies.<br />
    
    HTML:
    I want to convert them into clickable hyperlinks.
     
    scottlpool2003, Feb 28, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    And, do you want them to be clickable as is? Or do you have another parser converting the links with http in front of them to actual clickable links?
     
    PoPSiCLe, Feb 28, 2014 IP
  5. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #5
    Thanks for the reply. I'd like them to be clickable with just the www. There are thousands of records though so it will be impossible for me to know if they're all www or a mixture of www and http www
     
    scottlpool2003, Mar 3, 2014 IP