Wednesday, September 5, 2012

PHP startsWith() and endsWith() functions, Check String Ending/ Starting with a Character

PHP startsWith() and endsWith() functions, Check String Ending/ Starting with a Character 


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:


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



  1. 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.

  2. Bhavin Rana is a Professional & Experianced Freelance Developer From India.Thanks for sharing it.

    PHP Development India

  3. Good stuff man.. I'm about to learn something new.Want to build your website
    White Label Website Builder

  4. Great post. i like it. feeling great when reading your post.
    ecommerce website developers in hyderabad

  5. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
    Advanced AWS Training in Bangalore | Best Amazon Web Services Training Institute in Bangalore
    Advanced AWS Training Institute in Pune | Best Amazon Web Services Training Institute in Pune
    Advanced AWS Online Training Institute in india | Best Online AWS Certification Course in india
    AWS training in bangalore | Best aws training in bangalore

  6. 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. 
    microsoft azure training in bangalore
    rpa training in bangalore
    rpa training in pune
    best rpa training in bangalore

  7. This is very good content you share on this blog. it's very informative and provide me future related information.
    Best Devops online Training
    Online DevOps Certification Course - Gangboard

  8. I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
    python Course in Pune
    python Course institute in Chennai
    python Training institute in Bangalore


Any Questions or Suggestions ?


Professional & Experienced Freelance Developer From India, Technologist, Software Engineer, internet marketer and Open Sources Developer with experience in Finance, Telecoms and the Media. Contact Me for freelancing projects.

Enter your email address:

Delivered by FeedBurner

PHP Freelancer India - Google+