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 $.
\w (word character) matches any single letter, number or underscore (same as [a-zA-Z0-9_] ). The uppercase counterpart \W (non-word-character) matches any single character that doesn't match by \w (same as [^a-zA-Z0-9_] ). In regex, the uppercase metacharacter is always the inverse of the lowercase counterpart.
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1. 1* means any number of ones.
$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.
The plus ( + ) is a quantifier that matches one or more occurrences of the preceding element. The plus is similar to the asterisk ( * ) in that many occurrences are acceptable, but unlike the asterisk in that at least one occurrence is required.
It is a delimiter
A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With