1

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.

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

Community
  • 1
  • 1
  • To me it is not clear what you are asking. Maybe you can make your question clearer. However, you may want to have a look at mb_substr() as well. –  Apr 02 '15 at 10:28

3 Answers3

0
<?php
$property = $property['post_content'];
if (strlen($property) > 15) // if you want...
{
    $maxLength = 14;
    $property = substr($property, 0, $maxLength);
}
echo $property;
?>
Muhammad Ahmed
  • 481
  • 2
  • 14
0
<?php
if (strlen($property['post_content']) > 15) // if you want...
{
   $mystring= substr($property['post_content'], 0,14);
}
echo $mystring; 
?>
Kamran
  • 2,711
  • 2
  • 17
  • 24
  • user1374650 and Kamran Adil thank you for your replies i missed the echo how silly of me... Also if its not to much trouble how would i add it so it does not split the text mid sentence. – Phil Wilkinson Apr 02 '15 at 10:40
0

Try this

$property = (strlen($property) > 15) ? substr($property,0,14): $property ; 
Preeti Maurya
  • 431
  • 1
  • 7
  • 17