0

Not being great at regex I am trying to fix some code which errors on several preg_replace functions eg

$string = preg_replace('#(<[^>]+[\x00-\x20\"\'\/])(on|xmlns)[^>]*>#iUu',"$1>",$string);

The error value is 4 if thats any use. What would be helpful is a regex tester which shows what string format will pass the regexpression; no online regex testers do. Because the above function is returning an error, the return string is null. If anyone can hint what I need to change or look into appreciate.

Barmar
  • 741,623
  • 53
  • 500
  • 612
bigal
  • 47
  • 4
  • If you enable error reporting, what is the error message? – Barmar Aug 29 '14 at 02:55
  • 1
    it's not generally feasible to turn a regexp into a list of what matches it. – Barmar Aug 29 '14 at 02:56
  • 1
    That error code is [`PREG_BAD_UTF8_ERROR`](http://php.net/manual/en/pcre.constants.php) – Barmar Aug 29 '14 at 02:58
  • Description: Returned by preg_last_error() if the last error was caused by malformed UTF-8 data (only when running a regex in UTF-8 mode). – Barmar Aug 29 '14 at 02:59
  • So I guess there's a problem with the UTF-8 data in `$string`. I don't get any error when I try your regexp myself with an empty string. – Barmar Aug 29 '14 at 03:00
  • Also it looks like you're trying to parse XML with regexes, which is something one [should generally avoid](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – Dan Aug 29 '14 at 03:09
  • @derp Mine was just [the first search engine result](https://duckduckgo.com/?q=parsing+xml+with+regexes+php+stackoverflow) – Dan Aug 29 '14 at 03:18
  • Try to remove the "u" modifier to see what happens, it is probably useless. – Casimir et Hippolyte Aug 29 '14 at 05:02
  • >Try to remove the "u" modifier Did already. No change. If others are getting no errors could it be a server resource PHP problem eg may I need to define something else in PHP.ini eg memory_limit = 16M Consider that someone much more knowledgeable than I wrote the regex and its unlikely every instance of its use is failing. – bigal Aug 30 '14 at 03:36

0 Answers0