substr PHP shorten lengh of a string (WordPress Plugin)

I am a novice at PHP I am just learning. But I have come across a problem that I am having real trouble with.

I am working inside WordPress with a plug-in and I am trying to shorten a string within that plug-in so it just displays a small amount of text instead of a large section.

Read More

I know from reading this website and other including PHP.net that I need to use ‘substr’ The main problem is I think I need to add more than the string.

In my working one that does not shorten there is something with the string in [‘post_content’] I am not sure how that works with the syntax of PHP.

This is what I have come up with using the resources I found. Also adding … to the end would be great.

<?php

if (strlen($property) > 15) // if you want...
{
    $maxLength = 14;
    $property = substr($property, 0, $maxLength);
}
?>

And this is the working but without shortening

<?php echo $property['post_content']; ?> 

Resources

Get first n characters of a string

Shorten a text string in PHP

Shorten string with a “…” body

http://php.net/manual/en/function.substr.php

Related posts

Leave a Reply

3 comments

  1. <?php
    $property = $property['post_content'];
    if (strlen($property) > 15) // if you want...
    {
        $maxLength = 14;
        $property = substr($property, 0, $maxLength);
    }
    echo $property;
    ?>
    
  2. <?php
    if (strlen($property['post_content']) > 15) // if you want...
    {
       $mystring= substr($property['post_content'], 0,14);
    }
    echo $mystring; 
    ?>