Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

regular expression

Hi,

Say I have:

<?php
$tmp1 = "string5.php";
$tmp2 = "string51.php";
$tmp3 = "string506.php";
$tmp4 = "string_tmp.php";
?>

I need a regular expression check (preg_match) that returns true for $tmp1, $tmp2 and $tmp3 but returns false for $tmp4. The "string" and ".php" are stable, anything that follows the "string" has to checked whether it is a number of string. If its a number, return true else return false.

I am no good in regular expression. Thank you all.
0
sangeetha
Asked:
sangeetha
  • 3
  • 2
1 Solution
 
T22Commented:
Try this.

<?php
$tmp1 = "string5.php";
$tmp2 = "string51.php";
$tmp3 = "string506.php";
$tmp4 = "string_tmp.php";

function check ($str)
{
   return (preg_match ('/string\d*\.php/ ', $str)>0);
}

check($tmp1);

?>

0
 
sangeethaAuthor Commented:
Thanks, I assume \d*\ means 0 or more digits. Is it right?

There should be definitely, either string or number after the "string". So is this right:

 return (preg_match ('/string\d+\.php/ ', $str)>0);

I mean if:

$tmp5 = "string.php";

I want the preg_match to return false.

Thank you.
0
 
T22Commented:
Yes, you're right.
0
 
sangeethaAuthor Commented:
Thank you.
0
 
T22Commented:
You're welcome.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now