Three Missing String Functions

Written something you are proud of, post it here.
Post Reply
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Three Missing String Functions

Post by jacek »

These are three functions from my standard php library. they tend to be pretty useful as I get sick of having to constantly look up the order of parameters for strpos :)
function str_starts_with($needle, $haystack){
	$needle_len = strlen($needle);
	
	return ($needle_len <= strlen($haystack) && substr($haystack, 0, $needle_len) === $needle);
}

function str_ends_with($needle, $haystack){
	$needle_len = strlen($needle);
	
	return ($needle_len <= strlen($haystack) && substr($haystack, -$needle_len) === $needle);
}

function str_contains($needle, $haystack){
	return (strpos($haystack, $needle) !== false);
}
fairly self explanatory really.
Image
User avatar
Dylan
Posts: 150
Joined: Fri May 06, 2011 7:14 pm

Re: Three Missing String Functions

Post by Dylan »

Using a PHP library with user defined functions is a new concept to me, but I have slowly been building up one of my own.

They can really save time, and hassle and of course have the plus side of being totally user managed, therefore you control everything about it. Thanks for these functions, they will be help no doubt and definitely a sound addition to my library!
Post Reply