SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    823
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question preg_replace error

    Can someone tell me why I would be getting a preg_replace error with this borrowed code?

    I have a web page that is set as charset=utf-8; dbase is set the same way (using general). Information that comes from the dbase table has web URLs on it and this function is suppose to replace any text with 'http://' or 'www.' as a link address. I've tried to contact the creator, but no answer.

    Can someone give me a clue?
    Thank you.

    PHP Code:
    function urls2linksComplex($text$schemes null$tlds 'normal'){
      
    //"urls2links - Complex" function by Martin Pain / m-bread ( http://m-bread.com/resources/php/functions/urls2linksComplex )
      //This function can be distributed under the Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License
      //( http://creativecommons.org/licenses/by-sa/2.0/uk/ )
      //Please leave these comments intact.
      
    if($schemes == 'normal'){
        
    $scheme '(?:[Hh][Tt]|[Ff])[Tt][Pp][Ss]?';
      }elseif( 
    is_array($schemes) ){
        
    $scheme '(?:' implode('|'$schemes) . ')';
      }elseif( 
    is_string($schemes) ){
        
    $scheme $schemes;
      }else{
        
    $scheme '[a-zA-Z][a-zA-Z0-9\-+.]*';
      };
    //EoIF
      
    if($tlds == 'normal'){
        
    $tldExclude = array('doc''xls''txt''rtf''jpeg''jpg''gif''png''exe''html''htm''zip''gz''scr''rar''php''php3''inc''ico''bmp''asp''jsp''dat''lnk''cab''csv''xml''xsl''xsd''svg''psp''psd''pdf''bak''wav''mp3''m4v''midi''wmv''wma''js''css''ppt''pps''mdb');
      }elseif( 
    is_array($tlds) ){
        
    $tldExclude $tlds;
      }elseif( 
    is_string($tlds) ){
        
    $tldExclude = array($tlds);
      }else{
        
    $tldExclude = array();
      };
    //EoIF
        
    $userinfo '(?:(?:[a-zA-Z0-9\-._~!$&\'()*+,;=:]|%[0-9A-Fa-f]{2})*@)?';
          
    $decOctet '(?:[0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
         
    $ipv4 '(?:'.$decOctet.'\.){3}'.$decOctet;
         
    $regname '(?:(?:[0-9A-Za-z][0-9A-Za-z\-]*[0-9A-Za-z]|[0-9A-Za-z])\.)+[a-zA-Z]{2,6}';
        
    $host '('.$ipv4.'|'.$regname.')';
        
    $port '(?::[0-9]*)?';
       
    $authority '((?://)?'.$userinfo.$host.$port.')';
       
    $path '(?:/(?:[a-zA-Z0-9\-._~!$&\'()*+,;=:]|%[0-9A-Fa-f]{2})*?)*';
       
    $query '(?:\?(?:[a-zA-Z0-9\-._~!$&\'()*+,;=:/?]|%[0-9A-Fa-f]{2})*?)?';
       
    $fragment '(?:#(?:[a-zA-Z0-9\-._~!$&\'()*+,;=:/?]|%[0-9A-Fa-f]{2})*?)?';
      
    $pattern '\b(('.$scheme.'\:)?'.$authority.$path.$query.$fragment.')($|[^\w/][<\s]|[<\s]|[^\w/]$)';
      
    $replacement '( !in_array( substr(\'$4\', strrpos(\'$4\', \'.\')+1), $tldExclude) )?\'<a href="\'.((\'$2\' == \'\')?((strpos(\'$3\', \'@\'))?\'mailto:$1\':\'http://$1\'):\'$1\').\'">$1</a>$5\':\'$0\'';
      return 
    preg_replace(''.$pattern.'e'$replacement$text);
    };
    //EoFn urls2links 

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Thats rather complex and unwielding function there...
    Anyways I wrote one long ago to do the same thing:

    PHP Code:
    <?php

    define
    'LINK_LIMIT'30 );
    define'LINK_FORMAT''<a href="%s" rel="ext">%s</a>' );

    function 
    prase_links  $m )
    {
        
    $href $name html_entity_decode$m[0] );
        if ( 
    strpos$href'://' ) === false $href 'http://' $href;

        if( 
    strlen($name) > LINK_LIMIT ) {
            
    $k = ( LINK_LIMIT ) >> 1;
            
    $name substr$name0$k ) . '...' substr$name, -$k );
        }

        return 
    sprintfLINK_FORMAThtmlentities$href ), htmlentities$name ) );
    }

    $s 'Here is a text - www.ellehauge.net - it has some links with e.g. comma, www.one.com,
    in it. Some links look like this: http://mail.google.com - mostly they end with a
    space or carriage return www.unis.no
    <br /> - but they may also end with a period: http://ellehauge.net. You may even put
    the links in brackets (www.skred-svalbard.no) (http://one.com).
    From time to time, links use a secure protocol like https://gmail.com |
    This.one.is.a.trick. Sub-domaines: http://test.ellehauge.net |
    www.test.ellehauge.net | Files: www.unis.no/photo.jpg |
    Vars: www.unis.no?one=1&amp;~two=2 | No.: www.unis2_check.no/doc_under_score.php |
    www3.one.com | another tricky one:
    http://ellehauge.net/cv_by_id.php?id%5B%5D=105&amp;id%5B%5D=6&amp;id%5B%5D=100'
    ;

    $reg '~((?:https?://|www\d*\.)\S+[-\w+&@#/%=\~|])~i';
    print 
    preg_replace_callback$reg'prase_links'$s );
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    You are using a non-ASCII character as the delimiter (the broken bar character ). Change it to some non-alphanumeric (nor backslash) printable ASCII character which doesn't already exist in the pattern (unescaped): your best bet would be the backtick (`) character.

    In short, try simply changing the return line to be:
    PHP Code:
    return preg_replace('`'.$pattern.'`e'$replacement$text); 
    Salathe
    Software Developer and PHP Manual Author.

  4. #4
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    823
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Smile

    You know, I found out what it was.

    My web page is set for 'iso...', the database was set to 'latin...', but my insert forms charset was not set up correctly when it posted the information. I ran some experiments and found that out. WHEW!

    Now, regarding your response Salathe, I would've never thought that that would become an issue. Thanks for the suggestion. Maybe I should run another experiment...

    Thank you!


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
  •