function get_custom_excerpt( $content, $link ){
$content = some_function_to_handle_html_tag(substr($content, 0, 100)); // EDIT: need to be customized with a regex for proper output
$content .= ' <a href="'.$link.'"> more... </a>';
return $content;
}
function some_function_to_handle_html_tag(){
//a regex to check last occurance of html opening tag
//append respective closing tag or strip the tag if broken e.g. '<' or '<something'
}
//for global $post
echo get_custom_excerpt($post->post_content, get_permalink($post->ID)); //print the custom excerpt of 100 characters
or
//when in loop
echo get_custom_excerpt(get_the_content(), get_permalink(get_the_ID())); //print the custom excerpt of 100 characters
EDIT:
This could be unstable if some html tag was opened in first 100 character but not closed. I tried to figure out a proper solution (show content without stripping html) but found nothing helpful. But, I have an idea that may help –
1) You may use a regex which will check last occurance of a string which starts with <
and ends with >
, then you will have to add a respective closing tag in the end. So, say e.g. if there is tag <span>
, the </span>
will need to be added in the end to prevent html layout from breaking.
2) If any occurance like <
or <something
occurs, then that should be removed.
Then the above code should work for most of the cases.