i want to filter value
example:
112 to be 01:12
from script
$duration = get_post_meta( $post->ID, 'TMDB_Runtime', true );
if ( ! empty( $duration ) ) {
echo '<div class="gmr-duration-item" property="duration">' . $duration . __( ' <a class="fa fa-clock-o" aria-hidden="true"></a>','movies' ) . '</div>';
I've tried using substr and str_replace and put it into function.php
<?php
$text = '$duration';
$text = substr($text, 0, 3);
echo $text =str_replace("1","01:", $text);
?>
But always display parse error.
There are two problems I am noticing in this code snippet:
<?php
$text = '$duration';
$text = substr($text, 0, 3);
echo $text =str_replace("1","01:", $text);
?>
First:
echo $text =str_replace("1","01:", $text); is incorrect syntax. You can't assign a function to a function. Split it into two lines.
$text = str_replace("1","01:", $text);
echo $text;
Second:
$text = '$duration'; is not setting $text to 112. It is setting it to the literal string $duration. Single-quotes in PHP do not evaluate variables, that is what double-quotes do. So, try this instead:
$text = "$duration";
You could do it this way, statically:
<?php
$duration = "112";
$exploded = str_split($duration);
$exploded[0] = "0".$exploded[0].":";
$final = implode($exploded);
echo $final;
?>
Related
Hi Im getting Fatal error: Uncaught TypeError: Unsupported operand types: WP_Post + int . I really dont know why this is happening. Im using the below filter to display link and html elements i want on excerpt. also to limit the amount of word displayed on the excerpt. However, when used $number parameter i get the error even though the value is interger .
$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
this is the code that has the error.
so i put the the amount of word i want to display thorugh the apply filter below.
$topcontent = apply_filters( 'get_the_excerpt', '', 20 );
function custom_wp_trim_excerpt($text, $number) {
$raw_excerpt = $text;
// $countnumber = (int)($number);
// echo $countnumber;
if ( '' == $text ) {
//Retrieve the post content.
$text = get_the_content('');
//Delete all shortcode tags from the content.
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$allowed_tags = '<p>,<br>,<br/>,<br />,<a>,<em>,<strong>,<img>'; /*** MODIFY THIS. Add the allowed HTML tags separated by a comma.***/
$text = strip_tags($text, $allowed_tags);
$excerpt_word_count = $number;
/** MODIFY THIS. change the excerpt word count to any integer you like.***/
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
$excerpt_end = ' ' . '...' . '';
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
if ( count($words) > $excerpt_length && $words ) {
array_pop($words);
$text = implode(' ', $words);
$text = $text . $excerpt_more ;
} else {
$text = implode(' ', $words);
}
}
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'custom_wp_trim_excerpt', 10, 2);
I want to know why im not able to use the value i get from the apply filter parameter and how can i solve it?
the line below refers to a hook not an integer value!
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
you may try:
//count the excerpt
$exlen = strlen( strip_tags( get_the_excerpt() ) );
$words = preg_split("/[\n\r\t ]+/", $text, $exlen + 1, PREG_SPLIT_NO_EMPTY);
I would like to replace the word "custom" with
<span class="persProd">custom</span>.
This is my code but not work:
$output = '<span>Special custom products</span>';
$test = '~<span>custom</span>~';
$outputEdit = preg_replace($test, '<span class="persProd">custom</span>', $output);
echo $outputEdit;
How can i do?
Thanks for any help
I would do it like this. Watch out for 'custom' being in the $subject string twice. It will be replaced both times. I used spaces like so: ' custom '
$subject = '<span>Special custom products</span>';
$search = ' custom ';
$replace = '<span class="persProd"> custom </span>';
$outputEdit = str_replace($search, $replace, $subject);
echo $outputEdit;
Output: <span>Special<span class="persProd"> custom </span>products</span>
Here is the str_replace() page in the php manual for more.
This is my example and it will working not only with tags (some unique strings too).
<?php
function string_between_two_tags($str, $starting_tag, $ending_tag, $string4replace)
{
$start = strpos($str, $starting_tag)+strlen($starting_tag);
$end = strpos($str, $ending_tag);
return substr($str, 0, $start).$string4replace.substr($str, $end);
}
$output = '<span>Special custom products</span>';
$res = string_between_two_tags($output, '<span>', '</span>', 'custom');
echo $res;
?>
My first question here:
I am using this code to show excerpts on my Wordpress. It allows me to show tags within the excerpt and it works OK, but the problem is when a have a word that is linked, after it I always get one space before the comma.
Example:
Google, Yahoo
Output:
Google , Yahoo
The space after the comma is on purpose and it should be there. The one before is one too much.
Any tips on how to fix this?
Code that I use in order to define excerpt:
function new_wp_trim_excerpt($text) {
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text, '<a>');
$excerpt_length = apply_filters('excerpt_length', 70);
$words = preg_split('/(<a.*?a>)|\n|\r|\t|\s/', $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );
if ( count($words) > $excerpt_length ) {
array_pop($words);
$text = implode(' ', $words);
$text = $text . $excerpt_more;
} else {
$text = implode(' ', $words);
}
}
return apply_filters('new_wp_trim_excerpt', $text, $raw_excerpt);
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'new_wp_trim_excerpt');
It appears to be an issue with your regex, if you add a comma after the <a.*?a> does that help? Here's the regex line with the comma included:
$words = preg_split('/(<a.*?a>,)|\n|\r|\t|\s/', $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );
I'm trying to figure out if it's possible to get an excerpt from each post, grabbing the first paragraph from each one. I'm currently using the ACF plugin and have custom post types and custom fields.
Here's my code:
function custom_field_excerpt() {
global $post;
$text = get_field('news');
if ( '' != $text ) {
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$excerpt_length = 20; // 20 words
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
return apply_filters('the_excerpt', $text);
}
This works great, but it only trims the first 20 words (or however many words you specify), I'm trying to adjust this to pull in the first paragraph of each post instead of the first 20 words. Is this at all possible?
Because we know the content is bare (just text), you can simply explode the content via the \n character and then assume the first element in your new array is the first paragraph. You might be able to do this in a more efficient way but here is the function:
Your new function
function custom_field_excerpt() {
global $post;
$text = get_field('news');
if ( '' != $text ) {
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text_paragraphs = explode("\n",$text);
$text = $text_paragraphs[0];
}
return $text;
}
This is the correct code, add it to your functions.php file
// Add custom excerpt length
function custom_excerpt($excerpt_length) {
$content = get_field('custom_field_name');
$text = strip_shortcodes( $content );
//$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text);
$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
if ( count($words) > $excerpt_length ) {
array_pop($words);
$text = implode(' ', $words);
$text = $text . $excerpt_more;
} else {
$text = implode(' ', $words);
}
echo $text;
}
Then use < ?php custom_excerpt('12'); ?> to specify the length in your template
(Remove the space in the php tag above)
And for anyone else who may find this - if you are using the_content and not ACF then just change
$content = get_field('blog_article_text');
TO
$content = get_the_content();
Try to replace $excerpt_length with strlen($text)
$text = wp_trim_words( $text, strlen($text), $excerpt_more );
I have this interesting function that I'm using to create new lines into paragraphs. I'm using it instead of the nl2br() function, as it outputs better formatted text.
function nl2p($string, $line_breaks = true, $xml = true) {
$string = str_replace(array('<p>', '</p>', '<br>', '<br />'), '', $string);
// It is conceivable that people might still want single line-breaks
// without breaking into a new paragraph.
if ($line_breaks == true)
return '<p>'.preg_replace(array("/([\n]{2,})/i", "/([^>])\n([^<])/i"), array("</p>\n<p>", '<br'.($xml == true ? ' /' : '').'>'), trim($string)).'</p>';
else
return '<p>'.preg_replace(
array("/([\n]{2,})/i", "/([\r\n]{3,})/i","/([^>])\n([^<])/i"),
array("</p>\n<p>", "</p>\n<p>", '<br'.($xml == true ? ' /' : '').'>'),
trim($string)).'</p>';
}
The problem is that whenever I try to create a single line break, it inadvertently removes the first character of the paragraph below it. I'm not familiar enough with regex to understand what is causing the problem.
Here is another approach that doesn't use regular expressions. Note, this function will remove any single line-breaks.
function nl2p($string)
{
$paragraphs = '';
foreach (explode("\n", $string) as $line) {
if (trim($line)) {
$paragraphs .= '<p>' . $line . '</p>';
}
}
return $paragraphs;
}
If you only need to do this once in your app and don't want to create a function, it can easily be done inline:
<?php foreach (explode("\n", $string) as $line): ?>
<?php if (trim($line)): ?>
<p><?=$line?></p>
<?php endif ?>
<?php endforeach ?>
The problem is with your match for single line breaks. It matches the last character before the line break and the first after. Then you replace the match with <br>, so you lose those characters as well. You need to keep them in the replacement.
Try this:
function nl2p($string, $line_breaks = true, $xml = true) {
$string = str_replace(array('<p>', '</p>', '<br>', '<br />'), '', $string);
// It is conceivable that people might still want single line-breaks
// without breaking into a new paragraph.
if ($line_breaks == true)
return '<p>'.preg_replace(array("/([\n]{2,})/i", "/([^>])\n([^<])/i"), array("</p>\n<p>", '$1<br'.($xml == true ? ' /' : '').'>$2'), trim($string)).'</p>';
else
return '<p>'.preg_replace(
array("/([\n]{2,})/i", "/([\r\n]{3,})/i","/([^>])\n([^<])/i"),
array("</p>\n<p>", "</p>\n<p>", '$1<br'.($xml == true ? ' /' : '').'>$2'),
trim($string)).'</p>';
}
I also wrote a very simple version:
function nl2p($text)
{
return '<p>' . str_replace(['\r\n', '\r', '\n'], '</p><p>', $text) . '</p>';
}
#Laurent's answer wasn't working for me - the else statement was doing what the $line_breaks == true statement should have been doing, and it was making multiple line breaks into <br> tags, which PHP's native nl2br() already does.
Here's what I managed to get working with the expected behavior:
function nl2p( $string, $line_breaks = true, $xml = true ) {
// Remove current tags to avoid double-wrapping.
$string = str_replace( array( '<p>', '</p>', '<br>', '<br />' ), '', $string );
// Default: Use <br> for single line breaks, <p> for multiple line breaks.
if ( $line_breaks == true ) {
$string = '<p>' . preg_replace(
array( "/([\n]{2,})/i", "/([\r\n]{3,})/i", "/([^>])\n([^<])/i" ),
array( "</p>\n<p>", "</p>\n<p>", '$1<br' . ( $xml == true ? ' /' : '' ) . '>$2' ),
trim( $string ) ) . '</p>';
// Use <p> for all line breaks if $line_breaks is set to false.
} else {
$string = '<p>' . preg_replace(
array( "/([\n]{1,})/i", "/([\r]{1,})/i" ),
"</p>\n<p>",
trim( $string ) ) . '</p>';
}
// Remove empty paragraph tags.
$string = str_replace( '<p></p>', '', $string );
// Return string.
return $string;
}
Here's an approach that comes with a reverse method to replace paragraphs back to regular line breaks and vice versa.
These are useful to use when building a form input. When saving a users input you may want to convert line breaks to paragraph tags, however when editing the text in a form, you may not want the user to see any html characters. Then we would replace the paragraphs back to line breaks.
// This function will convert newlines to HTML paragraphs
// without paying attention to HTML tags. Feed it a raw string and it will
// simply return that string sectioned into HTML paragraphs
function nl2p($str) {
$arr=explode("\n",$str);
$out='';
for($i=0;$i<count($arr);$i++) {
if(strlen(trim($arr[$i]))>0)
$out.='<p>'.trim($arr[$i]).'</p>';
}
return $out;
}
// Return paragraph tags back to line breaks
function p2nl($str)
{
$str = preg_replace("/<p[^>]*?>/", "", $str);
$str = str_replace("</p>", "\r\n", $str);
return $str;
}
Expanding upon #NaturalBornCamper's solution:
function nl2p( $text, $class = '' ) {
$string = str_replace( array( "\r\n\r\n", "\n\n" ), '</p><p>', $text);
$string = str_replace( array( "\r\n", "\n" ), '<br />', $string);
return '<p' . ( $class ? ' class="' . $class . '"' : '' ) . '>' . $string . '</p>';
}
This takes care of both double line breaks by converting them to paragraphs, and single line breaks by converting them to <br />
Just type this between your lines:
echo '<br>';
This will give you a new line.