* Add a Base url to relative links in passed content. * * By default it supports the 'src' and 'href' attributes. However this can be * changed via the 3rd param. * * @since 2.7.0 * * @param string $content String to search for links in. * @param string $base The base URL to prefix to links. * @param array $attrs The attributes which should be processed. * @return string The processed content. */ function links_add_base_url( $content, $base, $attrs = array('src', 'href') ) { $attrs = implode('|', (array)$attrs); return preg_replace_callback("!($attrs)=(['\"])(.+?)\\2!i", create_function('$m', 'return _links_add_base($m, "' . $base . '");'), $content); } /** * Callback to add a base url to relative links in passed content. * * @since 2.7.0 * @access private * * @param string $m The matched link. * @param string $base The base URL to prefix to links. * @return string The processed link. */ function _links_add_base($m, $base) { //1 = attribute name 2 = quotation mark 3 = URL return $m[1] . '=' . $m[2] . (strpos($m[3], 'http://') === false ? path_join($base, $m[3]) : $m[3]) . $m[2]; } /** * Adds a Target attribute to all links in passed content. * * This function by default only applies to tags, however this can be * modified by the 3rd param. * * NOTE: Any current target attributed will be striped and replaced. * * @since 2.7.0 * * @param string $content String to search for links in. * @param string $target The Target to add to the links. * @param array $tags An array of tags to apply to. * @return string The processed content. */ function links_add_target( $content, $target = '_blank', $tags = array('a') ) { $tags = implode('|', (array)$tags); return preg_replace_callback("!<($tags)(.+?)>!i", create_function('$m', 'return _links_add_target($m, "' . $target . '");'), $content); } /** * Callback to add a target attribute to all links in passed content. * * @since 2.7.0 * @access private * * @param string $m The matched link. * @param string $target The Target to add to the links. * @return string The processed link. */ function _links_add_target( $m, $target ) { $tag = $m[1]; $link = preg_replace('|(target=[\'"](.*?)[\'"])|i', '', $m[2]); return '<' . $tag . $link . ' target="' . $target . '">'; } // normalize EOL characters and strip duplicate whitespace function normalize_whitespace( $str ) { $str = trim($str); $str = str_replace("\r", "\n", $str); $str = preg_replace( array( '/\n+/', '/[ \t]+/' ), array( "\n", ' ' ), $str ); return $str; } ?>