-1

I have a sample string:

revision
revision-2
revision-3
revision-n...
autosave

and php code

if(preg_match('/revision/', $str)) {
   echo "Edit 1";
}elseif(preg_match('/revision-(.+?)/', $str)) {
   echo "Edit $1";
}else {
   echo "Auto save";
}

But result null, how to fix it?

Hai Truong IT
  • 4,126
  • 13
  • 55
  • 102
  • http://www.9lessons.info/2013/10/understanding-regular-expression.html#more This is the best and easiest regular expression tutorial can help you in debugging – Veerendra Nov 13 '13 at 09:36
  • 1
    What do you mean by "result null"? This code echoes "Edit 1" when I execute it. What is is you are trying to achieve here? – ZeWaren Nov 13 '13 at 09:36
  • 1
    "Result null"? What does that mean? – deceze Nov 13 '13 at 09:36
  • maybe You have this problem: http://stackoverflow.com/questions/8859363/warning-preg-match-internal-pcre-fullinfo ??? – Adam Nov 13 '13 at 09:40

5 Answers5

1

You're not capturing your pattern anywhere. You need to do that:

$str = 'revision-14';
if(preg_match('/revision-(.*)/', $str, $matches)) 
{
   echo "Edit ".$matches[1];
}
elseif(preg_match('/revision/', $str)) 
{
   echo "Edit 1";
}
else 
{
   echo "Auto save";
}

also note, that /revision/ is less restrictive, so you need to place more-restrictive condition first

Alma Do
  • 37,009
  • 9
  • 76
  • 105
1

I think that what you are trying to do is this:

if(preg_match('/^revision-(?[0-9]+)$/', $str, $matches)) {
   echo "Edit ". $matches[0];
} elseif(preg_match('/revision/', $str)) {
   echo "Edit 1";
} else {
   echo "Auto save";
}

Explanation:

  • ̛'^' means 'starts with'
  • (? ) tells the regular expression to save the match it encloses
  • [0-9] means all characters from 0 to 9
  • '+' means one or more times
  • '$' means 'ends with'

Put that all together, and you have a regular expression meaning If the string contains only 'revision', a dash, and a number, save the number and output it as the Edit number. Otherwise output 'Auto Save'.

Tom Macdonald
  • 6,433
  • 7
  • 39
  • 59
0
  1. The first if(preg_match('/revision/', $str)) will match all the cases except "autosave". You may want /^revision$/ to match "revision" exclusively.
  2. I guess you expect echo "Edit $1" to output the (.+?) match. That's not how it works. You have to capture the match using the third argument to preg_match:

    if (preg_match('/revision-(.+?)/', $str, $match)) {
        echo "Edit $match[1]";
    }
    
deceze
  • 510,633
  • 85
  • 743
  • 889
0

Alternative:

You could use explode to find the value. If your output remains the same that is.

$match = end(explode('-', $str));

This will assign the value after '-' to $match.

Fyntasia
  • 1,133
  • 6
  • 19
0

There might be some problem in pattern string

hope below pattern will resolve ur problem.

preg_match('\'revision-2\'', 'revision revision-2 revision-3 revision-n... autosave', $matches);

There is one useful online link is also available to Perform a regular expression match. This can also be helpful for you :

http://www.functions-online.com/preg_match.html

Happy Coding :)

Kanisq
  • 232
  • 1
  • 5