-1

I am working with XML and using preg_replace_callback().

A question that comes up to my mind is: How do we write preg_replace_callback() patterns?

For example, "/{{{\s*"\s*(.+)\s*"\s*}}}/" or "/(<a .*)href=(")([^"]*)"([^>]*)>/U", what do any of these pattern do? How are these pattern written? What do the characters here mean?

hakre
  • 193,403
  • 52
  • 435
  • 836
furkan bozkurt
  • 133
  • 2
  • 14
  • 1
    The patterns are called regular expressions, or just RegEx, and there’s a [whole chapter on it here](https://www.php.net/manual/en/reference.pcre.pattern.syntax.php) and also a wonderful [online tool](https://regex101.com/) for experimenting. – Chris Haas Oct 09 '21 at 21:40
  • 3
    The tool to "work on an xml" isn't `preg_***` or anything related to the regexes, but `DOMDocument` or `XMLReader` or any tools designed to deal with XML in PHP. – Casimir et Hippolyte Oct 09 '21 at 21:52
  • 1
    Related: https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Tangentially Perpendicular Oct 09 '21 at 22:31
  • Does this answer your question? [How to use preg\_replace\_callback?](https://stackoverflow.com/questions/11174807/how-to-use-preg-replace-callback) – Leland Oct 11 '21 at 20:50

1 Answers1

2

Welcome to Stackoverflow. This seems to be a pretty fundamental question to me, therefore I'd like to answer it with some off-site resources as you already learned about Stackoverflow and could search it your own (it is a FAQ-like Question & Answer site):

  • These are, as you have rightfully identified your own, Patterns.
  • More specifically these are Regular Expression patterns (or called that way).
  • In concrete in PHP with preg_replace() they are in the Perl Compatible Regular Expression (PCRE) dialect.
  • The preg_* in front of preg_replace stands for (at least this is how I explain it to myself) Perl REGular expressions or as the PHP Manual words it "Regular Expressions (Perl-Compatible)", see https://www.php.net/pcre .

How is it written this pattern?

Like outlined in the PHP manual. (no pun intended)

What do the characters here mean?

The PHP manual has a short version (it is introductory) of the PCRE syntax. Start reading there (see PCRE Patterns (PHP Manual). The full syntax is documented with the PCRE library itself that you find available (bound) with PHP, for example the PCRE 2 manpage on the libraries homepage.

If you're using a supported PHP version, this is PCRE2. If not, it can vary. Nevertheless, the basics of those pattern are explained both in the PHP manual as well as on the PCRE website. It should provide enough text to read.

Remember that you can always create yourself small PHP scripts to try out things in PHP (and therefore with such patterns).

Additionally, literature exists available to study - even when offline and (out of electrical energy) at daylight (very comfortable!).

If you're out of luck finding literature, benefit from being online and having a printer at hand and print out the manual pages.

If you're lucky, your operating system ships with the documentation at pcre(3) or similar.

Good luck and thanks for asking!

hakre
  • 193,403
  • 52
  • 435
  • 836