简体   繁体   English

正则表达式(preg_match)

[英]Regular Expression (preg_match)

This is the not working 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>';        
}  
?>

What I want is that it should not display the link if it has a whitespace before or after. 我想要的是,如果它之前或之后有空格,它不应该显示链接。 So now it should display nothing. 所以现在它什么都不显示。 But it still displays the link. 但它仍然显示链接。

So, you don't want it to display if there's whitespaces. 所以,如果有空格,你不希望它显示。 Something like this should work, didn't test. 像这样的东西应该工作,没有测试。

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

This can also match ID12, because 3 is not an space, and the / of http:/ is not a space. 这也可以匹配ID12,因为3不是空格,而http:/不是空格。 You can try: 你可以试试:

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

You can try this. 你可以试试这个。 It works: 有用:

if (preg_match('%^\S*?/videosite\.com/(\w+)(?!\S+)$%i', $subject, $regs)) {
    #$result = $regs[0];
}

But i am positive that after I post this, you will update your question :) 但我很肯定,在我发布此帖后,您将更新您的问题:)

Explanation: 说明:

"
^            # Assert position at the beginning of the string
\S           # Match a single character that is a “non-whitespace character”
   *?           # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
\/           # Match the character “/” literally
videosite    # Match the characters “videosite” literally
\.           # Match the character “.” literally
com          # Match the characters “com” literally
\/           # Match the character “/” literally
(            # Match the regular expression below and capture its match into backreference number 1
   \w           # Match a single character that is a “word character” (letters, digits, etc.)
      +            # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
(?!          # Assert that it is impossible to match the regex below starting at this position (negative lookahead)
   \S           # Match a single character that is a “non-whitespace character”
      +            # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\$            # Assert position at the end of the string (or before the line break at the end of the string, if any)
"

It would probably be simpler to use this regex: 使用这个正则表达式可能更简单:

'/^http:\/\/videosite\.com\/(\w+)$/i'

I believe you are referring to the white space before http , and the white space after the directory. 我相信你指的是http之前的空格,以及目录之后的空格。 So, you should use the ^ character to indicate that the string must start with http, and use the $ character at the end to indicate that the string must end with a word character. 因此,您应该使用^字符来指示该字符串必须以http开头,并使用末尾的$字符表示该字符串必须以单词字符结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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