0

Can you combine a PHP function and string in the same echo statement? Currently, I have this code that grabs a photo's caption, then shortens it to 25 characters or less, stopping at the nearest blank space so it can fit the whole word.

echo substr($caption,0,strpos($caption,' ',25));
echo " ...";

Example output: changes "This is way too long to fit in this foobar small area preview box" to "This is way too long to..."

I'd like to be able to combine the 'substr' and '...' into the same echo statement. I tried the following, but it didn't work:

echo "{substr($caption,0,strpos($caption,' ',25))} ...";

Any ideas?

2 Answers 2

3

The , is great for this, and much faster than the ., which has the overhead of concatenating the string.

    echo substr($caption, 0, strpos($caption, ' ', 25)), '...';

EDIT: To clarify, the , just simply sends all the strings, separated by the comma, to echo, and thus is equal to the separate line echo statments. The DOT operator performs concatenation. You can use as many commas as you want, i.e. echo strFunction1(), ' some text ', strFunction2(), '...';

Sign up to request clarification or add additional context in comments.

Comments

0

Try:

echo substr($caption,0,strpos($caption,' ',25)) . '...';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.