PHP RegEx: Case Insensitive

Submitted by: 
Language: 
Visitors have accessed this post 658 times.

In previous tutorial we match at the beggining of the string given. But that is a case sensitive. If your condition like this: if(preg_match("/^ABC/", $string)) then the value of your string "abc". The script will returned with this message:

No match found

This is because of case sensitive. But in this script we will make the condition insensitive using regex "i" for insensitive. Shown below:

  1. <?php // create a string
  2. $string = 'Ronard';
  3. // try to match our pattern
  4. if(preg_match("/^ron/i", $string)) {
  5. // echo this is it matches
  6. echo 'The string begins with Ron';
  7. } else {
  8. // if not match is found echo this line
  9. echo 'No match found'; }
  10. ?>

Even if your string given are ROn, rOn, RoN the script will read insensitive and display the message "The string begins with Ron".


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.