Ver Mensaje Individual
  #18 (permalink)  
Antiguo 04/10/2012, 18:24
Avatar de Triby
Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: Saber si url de youtube son validos con php.

Wordpress usa esto:

Código PHP:
Ver original
  1. 1454 /**
  2. 1455     * Callback to convert URI match to HTML A element.
  3. 1456     *
  4. 1457     * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
  5. 1458     * make_clickable()}.
  6. 1459     *
  7. 1460     * @since 2.3.2
  8. 1461     * @access private
  9. 1462     *
  10. 1463     * @param array $matches Single Regex Match.
  11. 1464     * @return string HTML A element with URI address.
  12. 1465     */
  13. 1466    function _make_url_clickable_cb($matches) {
  14. 1467            $url = $matches[2];
  15. 1468   
  16. 1469            if ( ')' == $matches[3] && strpos( $url, '(' ) ) {
  17. 1470                    // If the trailing character is a closing parethesis, and the URL has an opening parenthesis in it, add the closing parenthesis to the URL.
  18. 1471                    // Then we can let the parenthesis balancer do its thing below.
  19. 1472                    $url .= $matches[3];
  20. 1473                    $suffix = '';
  21. 1474            } else {
  22. 1475                    $suffix = $matches[3];
  23. 1476            }
  24. 1477   
  25. 1478            // Include parentheses in the URL only if paired
  26. 1479            while ( substr_count( $url, '(' ) < substr_count( $url, ')' ) ) {
  27. 1480                    $suffix = strrchr( $url, ')' ) . $suffix;
  28. 1481                    $url = substr( $url, 0, strrpos( $url, ')' ) );
  29. 1482            }
  30. 1483   
  31. 1484            $url = esc_url($url);
  32. 1485            if ( empty($url) )
  33. 1486                    return $matches[0];
  34. 1487   
  35. 1488            return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $suffix;
  36. 1489    }
  37. 1490   
  38. 1491    /**
  39. 1492     * Callback to convert URL match to HTML A element.
  40. 1493     *
  41. 1494     * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
  42. 1495     * make_clickable()}.
  43. 1496     *
  44. 1497     * @since 2.3.2
  45. 1498     * @access private
  46. 1499     *
  47. 1500     * @param array $matches Single Regex Match.
  48. 1501     * @return string HTML A element with URL address.
  49. 1502     */
  50. 1503    function _make_web_ftp_clickable_cb($matches) {
  51. 1504            $ret = '';
  52. 1505            $dest = $matches[2];
  53. 1506            $dest = 'http://' . $dest;
  54. 1507            $dest = esc_url($dest);
  55. 1508            if ( empty($dest) )
  56. 1509                    return $matches[0];
  57. 1510   
  58. 1511            // removed trailing [.,;:)] from URL
  59. 1512            if ( in_array( substr($dest, -1), array('.', ',', ';', ':', ')') ) === true ) {
  60. 1513                    $ret = substr($dest, -1);
  61. 1514                    $dest = substr($dest, 0, strlen($dest)-1);
  62. 1515            }
  63. 1516            return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>$ret";
  64. 1517    }
  65. 1518   
  66. 1519    /**
  67. 1520     * Callback to convert email address match to HTML A element.
  68. 1521     *
  69. 1522     * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
  70. 1523     * make_clickable()}.
  71. 1524     *
  72. 1525     * @since 2.3.2
  73. 1526     * @access private
  74. 1527     *
  75. 1528     * @param array $matches Single Regex Match.
  76. 1529     * @return string HTML A element with email address.
  77. 1530     */
  78. 1531    function _make_email_clickable_cb($matches) {
  79. 1532            $email = $matches[2] . '@' . $matches[3];
  80. 1533            return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
  81. 1534    }
  82. 1535   
  83. 1536    /**
  84. 1537     * Convert plaintext URI to HTML links.
  85. 1538     *
  86. 1539     * Converts URI, www and ftp, and email addresses. Finishes by fixing links
  87. 1540     * within links.
  88. 1541     *
  89. 1542     * @since 0.71
  90. 1543     *
  91. 1544     * @param string $text Content to convert URIs.
  92. 1545     * @return string Content with converted URIs.
  93. 1546     */
  94. 1547    function make_clickable( $text ) {
  95. 1548            $r = '';
  96. 1549            $textarr = preg_split( '/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // split out HTML tags
  97. 1550            foreach ( $textarr as $piece ) {
  98. 1551                    if ( empty( $piece ) || ( $piece[0] == '<' && ! preg_match('|^<\s*[\w]{1,20}+://|', $piece) ) ) {
  99. 1552                            $r .= $piece;
  100. 1553                            continue;
  101. 1554                    }
  102. 1555   
  103. 1556                    // Long strings might contain expensive edge cases ...
  104. 1557                    if ( 10000 < strlen( $piece ) ) {
  105. 1558                            // ... break it up
  106. 1559                            foreach ( _split_str_by_whitespace( $piece, 2100 ) as $chunk ) { // 2100: Extra room for scheme and leading and trailing paretheses
  107. 1560                                    if ( 2101 < strlen( $chunk ) ) {
  108. 1561                                            $r .= $chunk; // Too big, no whitespace: bail.
  109. 1562                                    } else {
  110. 1563                                            $r .= make_clickable( $chunk );
  111. 1564                                    }
  112. 1565                            }
  113. 1566                    } else {
  114. 1567                            $ret = " $piece "; // Pad with whitespace to simplify the regexes
  115. 1568   
  116. 1569                            $url_clickable = '~
  117. 1570                                    ([\\s(<.,;:!?])                                        # 1: Leading whitespace, or punctuation
  118. 1571                                    (                                                      # 2: URL
  119. 1572                                            [\\w]{1,20}+://                                # Scheme and hier-part prefix
  120. 1573                                            (?=\S{1,2000}\s)                               # Limit to URLs less than about 2000 characters long
  121. 1574                                            [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]*+         # Non-punctuation URL character
  122. 1575                                            (?:                                            # Unroll the Loop: Only allow puctuation URL character if followed by a non-punctuation URL character
  123. 1576                                                    [\'.,;:!?)]                            # Punctuation URL character
  124. 1577                                                    [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]++ # Non-punctuation URL character
  125. 1578                                            )*
  126. 1579                                    )
  127. 1580                                    (\)?)                                                  # 3: Trailing closing parenthesis (for parethesis balancing post processing)
  128. 1581                            ~xS'; // The regex is a non-anchored pattern and does not have a single fixed starting character.
  129. 1582                                  // Tell PCRE to spend more time optimizing since, when used on a page load, it will probably be used several times.
  130. 1583   
  131. 1584                            $ret = preg_replace_callback( $url_clickable, '_make_url_clickable_cb', $ret );
  132. 1585   
  133. 1586                            $ret = preg_replace_callback( '#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret );
  134. 1587                            $ret = preg_replace_callback( '#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret );
  135. 1588   
  136. 1589                            $ret = substr( $ret, 1, -1 ); // Remove our whitespace padding.
  137. 1590                            $r .= $ret;
  138. 1591                    }
  139. 1592            }
  140. 1593   
  141. 1594            // Cleanup of accidental links within links
  142. 1595            $r = preg_replace( '#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', "$1$3</a>", $r );
  143. 1596            return $r;
  144. 1597    }

Elimina los números de línea y adáptalo para agregar la opción de los videos.
__________________
- León, Guanajuato
- GV-Foto