0

I have a web form that does a search replace function.

The form input is passed through preg_quote. That's been fine, until now.

Now I want to allow users to input the * (asterisk) wildcard character. preg_quote escapes it. How can I allow it through and properly apply it?

$find = ',*'; // I want to match a comma and all characters after the comma
$replace = ''; // replace with empty string
                    $find = preg_quote($find_replace[0],'~'); // $find == ',\*';. I want * to remain unescaped. Is replacing "\*" with "*" the best way?
                    if(strpos($find,'\\*') !== false) { // UPDATE: I tried that, and it does work.
                    // $find == ',\*'
                    $find = str_replace('\\*','*',$find);
                    // $find == ',*' -- seems like it should work but doesn't               
                }

                    $value = trim(preg_replace('~' . $find . '~i',$replace,$value));

EDIT: I added in code above that strips out the escaping slash before the *. It is working.

With $value = 'Hey, hey, hey, hey' I end up with 'Hey hey hey hey'. All the commas are removed, as expected with the regex pattern ,* (greedy matching?).

Note, in my code I'll be using $find = str_replace('\\*','.*',$find) or $find = str_replace('\\*','[\s\S]*',$find)to replicate Microsoft Excel Find/Replace functionality.

Buttle Butkus
  • 9,206
  • 13
  • 79
  • 120
  • 2
    Are you aware that `,*` is not the regular expression for "comma, and all characters after it"? The `*` here means "any number of the previous character", or "any number of commas" – Gareth Sep 12 '13 at 00:30
  • @Gareth Thank you. Yes, I am now aware of that as of about a minute ago when I refreshed my memory. Brain lapse. So yes, the pattern is working as it should with bwoebi's solution. But now I just have to change to code so that when the user enters `,*` it matches everything after the comma. I am trying to duplicate Excel-style find-replace in a web application. – Buttle Butkus Sep 12 '13 at 00:51
  • @Gareth And it took me a minute of googling to get that. This works: `$find = str_replace('\\*','.*',$find)` to replicate Excel functionality. But perhaps not quite. Excel probably matches newlines and that pattern doesn't. The solution in this answer probably works for that: http://stackoverflow.com/questions/4026213/php-regex-any-character ( `$find = str_replace('\\*','[\s\S]*',$find)` – Buttle Butkus Sep 12 '13 at 00:56

1 Answers1

1

I don't know better ways (don't think there are any…), but one way would be to just remove the backslash before asterisks after the preg_quote function call:

$find = str_replace("\\*", "*", $find);
bwoebi
  • 23,637
  • 5
  • 58
  • 79
  • This did not work. It caused text like "Hey, hey, hey, hey" to become "Hey hey hey hey". Any idea on that? – Buttle Butkus Sep 12 '13 at 00:22
  • Actually, this does work. My problem was that I was trying to replicate Excel functionality for * matching, and I forgot that regex works just a bit differently. Thanks! – Buttle Butkus Sep 12 '13 at 00:57