jeudi 2 avril 2015

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.


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://ift.tt/1ybpsGt


Aucun commentaire:

Enregistrer un commentaire