简体   繁体   中英

Regular Expression (preg_match_all)

This is my code:

    <?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>';        

    }  
    ?>

It's not working. I want to exclude every match that has a whitespace before or after the Link (with the ID). I used \\S for this.

For example if:

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

it should not display anything.

Thank you.

You are missing starting delimiter and an escape:

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

You are misplacing the leading slash in the regex. Try to use the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM