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 

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 :

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.

:D
Steel facing probs ? let me know by comments !

Know More About :
PHP Freelancing India

Reactions:

9 comments:

  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.

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

    PHP Development India

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

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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete

Any Questions or Suggestions ?

About

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+