Why is it empty for $ matches [0] because regular expressions are crazy? Write a regular expression, which can be matched normally. However, I still need to return the following preg_match (& nbsp; ^ & lt ;! -- SLayoutsnames * s * (. *) s -- & gt;, & nbsp; $ _ content, & nbsp; $ matches); $ _ content is & lt ;! -- Why is $ matches [0] empty when regular expressions get crazy?
Write a regular expression, which can be matched normally, but I still need it to return the entire matched string.
The regular expression is as follows:
preg_match( '/^
/', $_content, $matches);
$ _ Content is
I know that only the first one can be matched, but according to the official document:
$ Matches [0] will contain the text matching the full mode
However, after a long test, I can only get the following results:
Array
(
[0] =>
[1] => header
)
$ Matches [0] why is it empty? What about the matched text? (I originally thought that the value of $ matches [0] is )
There is another problem. I don't know why the assertion won't match,
preg_match( '/^
$/', $_content, $matches);
In this way, if a dollar sign is added at the end, nothing can be matched. Although this does not affect, I still want to know why and read the documents on the Internet, A bunch of terms are dizzy. I hope you can answer them. thank you.
Dizzy, how can I only give 100 points? I have 1000 points...
------ Solution ----------------------
$_content =<<< HTML
HTML;
preg_match( '/^
/', $_content, $matches);
print_r($matches);
Array
(
[0] =>
[1] => header
)
No problem
You are confused by yourself.
$ Matches [0] is an html tag that can be viewed only in text mode.
------ Solution ----------------------
$_content='
';
preg_match_all('/
/', $_content, $matches);
print_r($matches);
/*
Array
(
[0] => Array
(
[0] =>
[1] =>
)
[1] => Array
(
[0] => header
[1] => footer
)
)
*/
$ Matches [0] will contain the text matching the full modeIt has the following sentence:
$ Matches [1] will contain the text matched by the first capture sub-group, and so on.
This means that it only contains your anti-capture (that is, the content enclosed in your parentheses), rather than the global matching, so preg_match_all is used to perform global matching.
In addition, your regular expression is prefixed with ^, which indicates that the matching string must start with the content after ^. Yes.
Similarly, $ indicates that the matched string must end with $. therefore, to match the content, $ _ content =' '; Or $ _ content =' '; Can be matched
------ Solution ----------------------
^ Is the starting position for matching the input string. if the string to be matched does not start with the content after ^, the matching will not be executed at all.
If it is just judgment
The string does not start with bbs, This is faster
$ Str = '/bbs/www/csdn/net/xxx /';
If (strpos ($ str, 'bbs ')! = 0
------ Solution ----------------------
Strpos ($ str, 'bbs ') === false ){
// If it is equal to 0, it starts with bbs. if it is not found, false is returned (all equal to false)
Echo 'exe ';
}
------ Solution ----------------------
$ Str = 'www/bbs/net/xxx /';
If (preg_match ('/^ bbs. +? /', $ Str )){
Echo 'starts with bbs ';
} Else {
Echo 'exe ';
}