Visualizzazione dei risultati da 1 a 5 su 5

Discussione: preg_replace

  1. #1

    preg_replace

    salve io vorrei con il preg_replace sostituire tutti i link testuali presenti in un testo lungo scritti in questo tipo www.html.it o http://www.html.it o http://html.it in link reali di questo tipo www.html.it
    come posso fare?

  2. #2
    Vedi se questa funzione fa' al caso tuo:

    Codice PHP:
    /**
     * Auto-linker
     *
     * Automatically links URL and Email addresses.
     * Note: There's a bit of extra code here to deal with
     * URLs or emails that end in a period.  We'll strip these
     * off and add them after the link.
     *
     * @access    public
     * @param    string    the string
     * @param    string    the type: email, url, or both
     * @param    bool     whether to create pop-up links
     * @return    string
     */
    if ( ! function_exists('auto_link'))
    {
        function 
    auto_link($str$type 'both'$popup FALSE)
        {
            if (
    $type != 'email')
            {
                if (
    preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i"$str$matches))
                {
                    
    $pop = ($popup == TRUE) ? " target=\"_blank\" " "";

                    for (
    $i 0$i count($matches['0']); $i++)
                    {
                        
    $period '';
                        if (
    preg_match("|\.$|"$matches['6'][$i]))
                        {
                            
    $period '.';
                            
    $matches['6'][$i] = substr($matches['6'][$i], 0, -1);
                        }

                        
    $str str_replace($matches['0'][$i],
                        
    $matches['1'][$i].'<a href="http'.
                        
    $matches['4'][$i].'://'.
                        
    $matches['5'][$i].
                        
    $matches['6'][$i].'"'.$pop.'>http'.
                        
    $matches['4'][$i].'://'.
                        
    $matches['5'][$i].
                        
    $matches['6'][$i].'</a>'.
                        
    $period$str);
                    }
                }
            }

            if (
    $type != 'url')
            {
                if (
    preg_match_all("/([a-zA-Z0-9_\.\-\+]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)/i"$str$matches))
                {
                    for (
    $i 0$i count($matches['0']); $i++)
                    {
                        
    $period '';
                        if (
    preg_match("|\.$|"$matches['3'][$i]))
                        {
                            
    $period '.';
                            
    $matches['3'][$i] = substr($matches['3'][$i], 0, -1);
                        }

                        
    $str str_replace($matches['0'][$i], safe_mailto($matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i]).$period$str);
                    }
                }
            }

            return 
    $str;
        }

    L'ho presa da codeigniter (http://codeigniter.com)

    « Se nella prima mezz'ora non capisci chi è il pollo, allora il pollo sei tu. » [Thomas "Amarillo Slim" Preston, campione del mondo di poker]

  3. #3
    l'idea è ottima ma non funziona. mida sempre un problema di questo tipo
    preg_match_all() [function.preg-match-all]: Compilation failed: range out of order in character class at offset
    puoi verificare?

  4. #4
    che versione di php stai usando??

    io uso quotidianamente questa funzione su tutti i miei siti senza alcun tipo di problema...
    « Se nella prima mezz'ora non capisci chi è il pollo, allora il pollo sei tu. » [Thomas "Amarillo Slim" Preston, campione del mondo di poker]

  5. #5

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.