如何使用preg_match从字符串中搜索,包括符号?

时间:2013-01-16 10:09:39

标签: php preg-match

我有一个模式status: available,但冒号符号无论如何都不起作用。如何修改这个模式?

我在代码中弄乱了一些东西,当我找到它时会通知你。谢谢

好的,我打破了代码。我正在编写一个域可用性的脚本。

<?php
$server = 'whois.cira.ca';
$pattern = 'status: available';

$domain = 'nonexistingdomain';
$extension = '.ca';

$buffer = NULL;
$sock = fsockopen($server, 43) or die('Error Connecting To Server: ' . $server);
fputs($sock, $domain.$extension . "\r\n");

while( !feof($sock) )
{
    $buffer .= fgets($sock,128);
}

//If I give a value localy to $buffer (like below) it works, but if $buffer takes the value from fgets() function it wont
$buffer = "Domain name: nonexistingdomain.ca Domain status: available % WHOIS look-up made at 2013-01-16 12:35:45 (GMT) % % Use of CIRA's WHOIS service is governed by the Terms of Use in its Legal % Notice, available at http://www.cira.ca/legal-notice/?lang=en % % (c) 2013 Canadian Internet Registration Authority, (http://www.cira.ca/) NO";

fclose($sock);

if(preg_match("/$pattern/", $buffer))
    echo "YEP";
else
    echo "NO";
?>

如果我将$ pattern更改为“available”就可以了!

1 个答案:

答案 0 :(得分:0)

好像你错过了一个分隔符。

试试这个:

<?php

$pattern = '/status: available/';

$string = "String1: status: available";
$string1 = "String2: status: unavailable";


if (preg_match($pattern,$string))
    echo 'String1 matches<br>';
else
    echo 'String1 does not match<br>';

if (preg_match($pattern,$string1))
    echo 'String2 matches<br>';
else
    echo 'String 2 does not match<br>';
?>

提供以下输出:

String1 matches

String 2 does not match