Author Topic: need help with preg_match  (Read 2108 times)

0 Members and 1 Guest are viewing this topic.

Offline jeplerTopic starter

  • Irregular
  • Gender: Male
    • View Profile
    • http://www.mytechmusings.com
need help with preg_match
« on: February 06, 2007, 09:09:20 AM »
Hello,
I'm trying to match a pattern that starts with the letters "tn" but could have a variety of characters after. I tried the following but it doesn't seem to work:

if (preg_match("^tn%",$myword)) { do something...

Offline Tyche

  • Irregular
  • Gender: Female
    • View Profile
Re: need help with preg_match
« Reply #1 on: February 06, 2007, 09:20:05 AM »
You need to put your Regex Pattern in delimiters,  try
Code: [Select]
if (preg_match("/^tn%/",$myword)) { do something...

Or use strpos  - it's a bit more efficient in this case

e.g.
Code: [Select]
if (strpos($myword,"tn")===0) { do something...

Offline mjdamato

  • Guru
  • Fanatic
  • *
    • View Profile
Re: need help with preg_match
« Reply #2 on: February 06, 2007, 09:22:24 AM »
Remove the % sign as well:

if (preg_match("/^tn/", $myword))
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline jeplerTopic starter

  • Irregular
  • Gender: Male
    • View Profile
    • http://www.mytechmusings.com
Re: need help with preg_match
« Reply #3 on: February 06, 2007, 09:04:54 PM »
excellent! thanks!!

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.