Tango Alpha
Tango Alpha

Reputation: 157

substr function not returning desired substring

Suppose I have following string :

$str = "PHP code may be embedded into HTML or HTML5 markup, or it can be used in combination with various web template systems, web content management systems and web frameworks.";

Here, I'm printing the substring starting from the word 'combination', and ending upto 'content'

echo substr($str, strpos($str, 'combination'), strpos($str, 'content'));

It is printing from 'combination' and to the end, instead of upto 'content'. Is there anything wrong in the third parameter ?

Upvotes: 0

Views: 172

Answers (1)

splash58
splash58

Reputation: 26153

The third argument for substr should be a string length but not the end position

$i = strpos($str, 'combination');
echo substr($str, $i, strpos($str, 'content') - $i);

Upvotes: 5

Related Questions