#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,689
    Rep Power
    171

    A Regex quesiton. preg_replace_callback breaks the link into 2 lines!


    Hello I use osticket.com to create support tickets. It works fin but the link sent to the clients is broken! I end up with link like this:

    For example this one is in 2 different lines. Notice the whitespace after @site before .com that causes line break
    http://www.site.com.au/support/view.php?t=881119&e=lance@site
    .com.au&a=2d96be3bfe455ee475e861a05d1922c6.
    Or this one is not even a link and comes across as a plain text:
    http://www.site.com.au/support/view.php?t=440307&e=pmdg2@yahoo.com&a=1a141c1e356cfbce86ba3f6e5c5771d9.
    How can I change the code so it works fine? Thanks:

    PHP Code:
    //make urls clickable. Mainly for display
        
    function clickableurls($text) {
            global 
    $ost;

            
    $token $ost->getLinkToken();
            
    //Not perfect but it works - please help improve it.
            
    $text=preg_replace_callback('/(((f|ht){1}tp(s?):\/\/)[-a-zA-Z0-9@:%_\+.~#?&;\/\/=]+)/',
                    
    create_function('$matches',
                        
    sprintf('return "<a href=\"l.php?url=".urlencode($matches[1])."&auth=%s\" target=\"_blank\">".$matches[1]."</a>";',
                            
    $token)),
                    
    $text);

            
    $text=preg_replace_callback("/(^|[ \\n\\r\\t])(www\.([a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+)(\/[^\/ \\n\\r]*)*)/",
                    
    create_function('$matches',
                        
    sprintf('return "<a href=\"l.php?url=".urlencode("http://".$matches[2])."&auth=%s\" target=\"_blank\">".$matches[2]."</a>";',
                            
    $token)),
                    
    $text);

            
    $text=preg_replace("/(^|[ \\n\\r\\t])([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4})/",
                
    '\\1<a href="mailto:\\2" target="_blank">\\2</a>'$text);

            return 
    $text;
        } 
  2. #2
  3. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,689
    Rep Power
    171
    Hi anyone? Any ideas?
    Thanks
  4. #3
  5. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    How is it being sent to the user? Website or through email?

    I know some emails can have forced wrapping applied and that .com might be seen as the first opportunity to wrap to avoid exceeding 80 characters.

    Coincidentally I've seen RT (another ticketing system) apply this kind of wrapping even when taking in customer email content.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    You must urlencode() items in URLs. @ is a reserved character in URLs for URLs such as http://maniacdan:P4ssw0rd@www.somesite.com/folder/file.php
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by ManiacDan
    You must urlencode() items in URLs. @ is a reserved character in URLs for URLs such as http://maniacdan:P4ssw0rd@www.somesite.com/folder/file.php
    I think he is?

    In the first:
    href=\"l.php?url=".urlencode($matches[1])."&auth=%s\"

    In the second:
    href=\"l.php?url=".urlencode("http://".$matches[2])."&auth=%s\"

    Not relevant in the third since it's limited to an email address.

    So anytime @ would be in the original text, it'd be urlencoded before being put within the output.
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Yeah, maybe. he also has $token in the second callback when he probably means $text. Maybe.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,689
    Rep Power
    171
    Originally Posted by dmittner
    How is it being sent to the user? Website or through email?

    I know some emails can have forced wrapping applied and that .com might be seen as the first opportunity to wrap to avoid exceeding 80 characters.

    Coincidentally I've seen RT (another ticketing system) apply this kind of wrapping even when taking in customer email content.
    With email.
  14. #8
  15. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,689
    Rep Power
    171
    Originally Posted by ManiacDan
    You must urlencode() items in URLs. @ is a reserved character in URLs for URLs such as http://maniacdan:P4ssw0rd@www.somesite.com/folder/file.php
    This made no differnece:
    PHP Code:
    //make urls clickable. Mainly for display
        
    function clickableurls($text) {
            global 
    $ost;

            
    $token $ost->getLinkToken();
            
    //Not perfect but it works - please help improve it.
            
    $text=preg_replace_callback('/(((f|ht){1}tp(s?):\/\/)[-a-zA-Z0-9@:%_\+.~#?&;\/\/=]+)/',
                    
    create_function('$matches',
                        
    sprintf('return "<a href=\"l.php?url=".urlencode($matches[1])."&auth=%s\" target=\"_blank\">".$matches[1]."</a>";',
                            
    $token)),
                    
    $text);

            
    $text=preg_replace_callback("/(^|[ \\n\\r\\t])(www\.([a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+)(\/[^\/ \\n\\r]*)*)/",
                    
    create_function('$matches',
                        
    sprintf('return "<a href=\"l.php?url=".urlencode("http://".$matches[2])."&auth=%s\" target=\"_blank\">".$matches[2]."</a>";',
                            
    $token)),
                    
    $text);

            
    $text=preg_replace("/(^|[ \\n\\r\\t])([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4})/",
                
    '\\1<a href="mailto:\\2" target="_blank">\\2</a>'$text);

            return 
    $text;
        }

        function 
    stripEmptyLines ($string) {
            
    //return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $string);
            //return preg_replace('/\s\s+/',"\n",$string); //Too strict??
            
    return preg_replace("/\n{3,}/""\n\n"$string);
        }


        function 
    linebreaks($string) {
            return 
    urldecode(ereg_replace("%0D"" "urlencode($string)));
        } 
  16. #9
  17. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by zxcvbnm
    With email.
    One thing you could try is completely ignoring the below logic and see what happens if the content of the email naturally has that long URL string in it. If it wraps the same way then you know it's not a problem with this code, and might be an email setting.

IMN logo majestic logo threadwatch logo seochat tools logo