PHP startsWith() and endsWith() functions, Check String Ending/ Starting with a Character
Q:I need two functions that would take a string and return if it starts with the specified character/string or ends with it.
For example:
$str='|apples}';
echo startsWith($str,'|'); //Returns true
echo endsWith($str,'}'); //Returns true
Solution :
echo startsWith($str,'|'); //Returns true
echo endsWith($str,'}'); //Returns true
Solution :
function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
function endsWith($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
Use this if you don't want to use a regex.
Steel facing probs ? let me know by comments !
Know More About :
PHP Freelancing India
Thanks for sharing those informations that giving us a lot of ideas on doing PHP functions. This must be very helpful and every php programmer can use this content.
ReplyDeleteBhavin Rana is a Professional & Experianced Freelance Developer From India.Thanks for sharing it.
ReplyDeletePHP Development India
Thanks for the information, keep updating.
ReplyDeleteWeb Design company in Hubli | web designing in Hubli | SEO company in Hubli
Good stuff man.. I'm about to learn something new.Want to build your website
ReplyDeleteWhite Label Website Builder
Great post. i like it. feeling great when reading your post.
ReplyDeleteecommerce website developers in hyderabad
I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog.
ReplyDeletemicrosoft azure training in bangalore
rpa training in bangalore
rpa training in pune
best rpa training in bangalore
This is very good content you share on this blog. it's very informative and provide me future related information.
ReplyDeleteBest Devops online Training
Online DevOps Certification Course - Gangboard
Thanks For sharing Your information The information shared Is Very Valuable Please Keep Updating Us Time Just went On reading Thae article Python Online Training DataScience Online Training AWS Online Training Hadoop Online Training
ReplyDeleteThanks for this wonderful blog it is really informative to all.keep update more information about this...
ReplyDeleteTally Course in Bangalore
Tally Training in Bangalore
This post is so helpfull and informative.keep updating with more information...
ReplyDeleteDigital Marketing Concepts
Media Promotion
This post is so interactive and informative.keep update more information...
ReplyDeleteTypes of Oracle Database Users
Different Types of Oracle Database Users
Happy to read the informative blog. Thanks for sharing
ReplyDeletebest training institute for selenium in chennai
best selenium testing training in chennai
This post is so usefull and informative.keep updating with more information...
ReplyDeleteComputer Systems Testing
Benefits Of Test Automation
Awesome blog. Thanks for sharing this blog. Keep update like this...
ReplyDeleteAndroid Classes in Hyderabad
Android Training in Gurgaon
This post is so interactive and informative.keep update more information...
ReplyDeleteEthical Hacking Course in Tambaram
Ethical Hacking Course in Chennai
Mindblowing blog very useful thanks
ReplyDeleteDevOps Training in Porur
DevOps Training in Chennai