Domanda

What does the "~" character mean in the following?:

preg_match_all("~<img [^>]+>~", $inputw, $output);

My guess is that they are beginning and end markers such as ^ and $.

È stato utile?

Soluzione

It is a delimiter

A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.

Altri suggerimenti

As Nambi said you are free to choose the delimiter in a regex. However if the delimiter appears in the pattern it has to escaped. Knowing this, imagine the following situation

'/\/var\/www\/test/' # delimited with /
'~/var/www/test~' # delimited with ~

The last one does not require to escape the / as the delimiter is now ~. Much cleaner isn't it?

As a general guideline you are encouraged to choose a delimiter which isn't pattern of the pattern itself, I guess ~ is widely distributed as an alternative to / as it rarely appears in real world pattern.

The dirty little delimiter secret they don't tell you ->
http://uk.php.net/manual/en/regexp.reference.delimiters.php

Examples:

Paired delimiters (raw: \d{2}Some\{33\}\w{5})

{\d{2}Some\\{33\\}\w{5}} parses to \d{2}Some\\{33\\}\w{5} and
{\d{2}Some\{33\}\w{5}} parses to \d{2}Some{33}\w{5}

Un-Paired delimiters (raw: \d{2}Some\+33\+\w{5})

+\d{2}Some\+33\+\w{5}+ parses to \d{2}Some+33+\w{5} and
+\d{2}Some\\+33\\+\w{5}+ won't parse because the delimiter is un-escaped.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top