您可以試試這個。它的工作原理:
if (preg_match('%^\S*?/videosite\.com/(\w+)(?!\S+)$%i', $subject, $regs)) {
#$result = $regs[0];
}
但我肯定我張貼此之後,您將更新你的問題:)
說明:
"
^ # 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)
"
根據你的問題,如果它符合reg exp它不應該顯示任何內容,而你正在做相反的事情。如果開始和結束有空白區域,請打印。 :) – mithunsatheesh