正则表达式(preg_match_all)

时间:2011-11-23 17:32:06

标签: php preg-match whitespace preg-match-all

这是我的代码:

    <?php
    $matchWith = "http://videosite.com/ID123";
    preg_match_all('/\S\/videosite\.com\/(\w+)\S/i', $matchWith, $matches);  
    foreach($matches[1] as $value)
    {  
            print '<a href="http://videosite.com/'.$value.'">
    Hyperlink
    </a>';        

    }  
    ?>

它不起作用。 我想排除在链接之前或之后具有空格的所有匹配(具有ID)。 我使用了\S

例如:if:

$matchWith = " http://videosite.com/ID123 ";

它不应该显示任何内容。

谢谢。

2 个答案:

答案 0 :(得分:0)

您缺少起始分隔符和转义符:

preg_match_all('/\S\/videosite\.com\/(\w+)\S/i', $matchWith, $matches);  
                ^  ^

答案 1 :(得分:0)

你正在错误地使用正则表达式中的前导斜杠。 尝试使用以下内容:

preg_match_all('/\Svideosite\.com\/(\w+)\S/i', $matchWith, $matches);
相关问题