Looking for a bunch of things really.
Essentially what I’m looking to create is the following string which can only contain MAX 140 chars (see where I’m going with this):
"Some text here from an XML feed... #tweeted http://bit.ly/123456"
So I already have a $text variable which contains the text from the XML feed, this text string will always contain the string #tweeted and can be of any length.
Lets say thats 200 chars for this example, including the hashtag.
I know need to make room for the ...<space> and http://bit.ly/123456 (24 Chars in total)
-
Remove the #hashtag E.g:
$removehash = str_replace("#tweeted", " ", $text); -
Get the length E.g.
$length = strlen($text); -
Trim it to 116, if over 140 chars.
-
If under 116 chars do nothing because there is space for the … and the link
-
add the … and the URL ($url).
Any advice, if you’ve done this previously, or any ideas on how to trim the string, remove the hashtag and apend to the end of the string to contrain it to a max of 140 chars greatly appreciated.
Not sure how to trim the text?
PHP’s substr function returns a portion of a string so you can, for example, get the first 116 chars of a string by
It sounds like you are taking a 140 char string + #tweeted + URL and wanting to produce an output string of not greater than 140 chars? I would try something like:
Obviously I’ve used too many temporary variables but hopefully that makes it clearer. Would also need to validate input string if necessary or the first explode will not return the expected result. The 139 in the sub is 140 char output – 1 char for the space in the result string.
The substr line takes into account that the bitly URLs may be of different lengths and doesn’t just blindly cut off at 116 chars.