I can’t seem to figure out what I’m doing wrong…
I’m trying to find matches of
<cite>stuffhere</cite>
Is this right?
preg_match_all('<cite>(.*?)</cite>/ms', $str, $matches)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your confusion is not your fault; PHP is notoriously weird in this area.
In most programming languages, you create a regex object one of two ways. If the language supports regexes as a first-class language element, you can use a regex literal:
Here, the forward-slash (
/) is the regex delimiter; if you want to match a literal/, you have to escape it with a backslash:\/.In other languages, you have to write the regex in the form of a string literal, which you then pass to a constructor or a factory method:
The forward-slash doesn’t need to be escaped, but both the double-quote (
") and backslash (\) do, because of their special meanings in string literals.But PHP is unique: it doesn’t support regex literals, so you have to write the regex as a string, but the string has to look like a regex literal! That is, it has to have string delimiters (quotes) and regex delimiters. For example:
It isn’t all bad; as you can see, you can use PHP’s single-quoted strings instead of double-quoted, so you don’t have to escape all backslashes and double-quotes. You can also choose different regex delimiters, so you don’t have to escape (for example) literal forward-slashes in your regex:
The modifiers (‘s’ for single-line, ‘i’ for ignore-case, etc.) go after the trailing regex delimiter, as in Perl or JavaScript. Almost any ASCII punctuation character can be used as a regex delimiter;
~and#are popular choices.