开发者

what does @ mean when used in preg_match?

开发者 https://www.devze.com 2023-02-22 06:07 出处:网络
This is from a class there is a @ sign in preg_match what does it mean or its purpose? Does it mean a space?

This is from a class there is a @ sign in preg_match what does it mean or its purpose? Does it mean a space?

if (preg_match("@Property Information </td>@",simplexml_import_dom($cols->item(0))->asXML(),$ok))
{
     $table_name = 'Property Informa开发者_Go百科tion';
}


In that case, it is being used as a pattern delimiter. As that manual page says,

When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.

Often used delimiters are forward slashes (/), hash signs (#) and tildes (~).


It is just a delimiter. It can be any other pair of character. The following are all the same

"@Property Information </td>@"
"+Property Information </td>+"
"|Property Information </td>|"
"#Property Information </td>#"
"[Property Information </td>]"
...

The purpose of the delimiter to separate regex pattern with modifier, e.g. if you need case-insensitive match you'll put an i after the delimiter, e.g.

"@Property Information </td>@i"
"+Property Information </td>+i"
"|Property Information </td>|i"
"#Property Information </td>#i"
"[Property Information </td>]i"
...

See http://www.php.net/manual/en/regexp.reference.delimiters.php for detail.


Almost any character - when appearing at the first position - can be used as a PCRE delimiter. In this case it's the @ (another common one would be / but when dealing with closing tags that one is not really good as you'd have to escape every / in the text )

See http://www.php.net/manual/en/regexp.reference.delimiters.php for details.


However, you shouldn't use a Regex for this check at all - you are just testing if a plain string is in another string. Here's a proper solution:

$xml = simplexml_import_dom($cols->item(0))->asXML()
if(strpos($xml, 'Property Information </td>') !== false) { ... }

Actually, using string operators when dealing with html/xml is not really nice but if you are just doing simple "contains" checks it's usually the easiest way.


every regular expression must start and end with the same character. the author of the given regular expression has chosen to start and end the regular expression with an @ sign.

0

精彩评论

暂无评论...
验证码 换一张
取 消