[PHP] truncating a string after a substring

Recommended Posts

coldacid    755
Okay, I have a nice happy string, and I want to cut it short. I want to have everything up to and including the first instance of "</p>" in the string.
substr($content, 0, strpos($content, '</p>')) . '</p>'

should do the trick, but passing 0 as length to substr is not documented (in case there isn't a '</p>' in \$content). On top of that, I don't know if I've made a fencepost error or not. If someone could try this out and get back to me; I'm currently busy debugging SQL in the script.

Share on other sites
coldacid    755
Never mind. Turns out a 0 is treated as a positive number, so I got nothing. Had to use an if statement to check if the para close was in there. Works, though, and nicely.