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

16 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. 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
  6. 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
  7. 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

    ReplyDelete
  8. Thanks for this wonderful blog it is really informative to all.keep update more information about this...
    Tally Course in Bangalore
    Tally Training in Bangalore

    ReplyDelete
  9. This post is so helpfull and informative.keep updating with more information...
    Digital Marketing Concepts
    Media Promotion

    ReplyDelete
  10. This post is so usefull and informative.keep updating with more information...
    Computer Systems Testing
    Benefits Of Test Automation

    ReplyDelete
  11. Awesome blog. Thanks for sharing this blog. Keep update like this...
    Android Classes in Hyderabad
    Android Training in Gurgaon

    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