Exploit
Exploit

Reputation: 6386

what does @ mean when used in preg_match?

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 Information';
}

Upvotes: 0

Views: 342

Answers (4)

davogotland
davogotland

Reputation: 2777

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.

Upvotes: 1

ThiefMaster
ThiefMaster

Reputation: 318808

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.

Upvotes: 1

salathe
salathe

Reputation: 51970

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 (~).

Upvotes: 1

kennytm
kennytm

Reputation: 523784

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.

Upvotes: 1

Related Questions