PHP Regular Expression to find the first youtube link
I'm trying to find an expression to keep only the first youtube link I find in $render variable.
$render="some text here https://www.youtube.com/watch?v=fJ9rUzIMcZQ https://www.youtube.com/watch?v=fJ9rUzIMcZQ some text here https://www.youtube.com/watch?v=fJ9rUzIMcZQhttps://www.youtube.com/watch?v=fJ9rUzIMcZQ";
$prefix = "https://www.youtube.com/watch?v=";
$index = strpos($render, $prefix) + strlen($prefix);
$youtube = substr($render, $index);
$youtube = strtok($youtube,' ');
$regex="@(https)://(www.)?youtube.com/watch?v=[w_-].* *@";
preg_match($string, $render, $matches, PREG_OFFSET_CAPTURE);
$render = preg_replace($regex, "", $render);
$render = substr_replace($render, $matches[0][0], $matches[0][1], 0);
echo $render;
What I get
https://www.youtube.com/watch?v=fJ9rUzIMcZQ ://www.youtube.com/watch?v=fJ9rUzIMcZQ
What I want to get
https://www.youtube.com/watch?v=fJ9rUzIMcZQ
P.S.
The last two links are combined
php regex hyperlink youtube
add a comment |
I'm trying to find an expression to keep only the first youtube link I find in $render variable.
$render="some text here https://www.youtube.com/watch?v=fJ9rUzIMcZQ https://www.youtube.com/watch?v=fJ9rUzIMcZQ some text here https://www.youtube.com/watch?v=fJ9rUzIMcZQhttps://www.youtube.com/watch?v=fJ9rUzIMcZQ";
$prefix = "https://www.youtube.com/watch?v=";
$index = strpos($render, $prefix) + strlen($prefix);
$youtube = substr($render, $index);
$youtube = strtok($youtube,' ');
$regex="@(https)://(www.)?youtube.com/watch?v=[w_-].* *@";
preg_match($string, $render, $matches, PREG_OFFSET_CAPTURE);
$render = preg_replace($regex, "", $render);
$render = substr_replace($render, $matches[0][0], $matches[0][1], 0);
echo $render;
What I get
https://www.youtube.com/watch?v=fJ9rUzIMcZQ ://www.youtube.com/watch?v=fJ9rUzIMcZQ
What I want to get
https://www.youtube.com/watch?v=fJ9rUzIMcZQ
P.S.
The last two links are combined
php regex hyperlink youtube
Is$rendera string filled with links, each link separated from the others by a space?
– Terminus
Nov 22 at 23:57
Yes. They are separated with space in except the last two. They are combined.
– Varkoume
Nov 23 at 0:01
1
And if you only want the first link, why not simplyexplode()into an array, grab the first element, and confirm that it is a youtube URL ? If not, grab next and repeat.
– ivanivan
Nov 23 at 0:09
Sorry for not describing correctly what I want. There's possibility of containing and other words except of links. Like: $render="some text here youtube.com/watch?v=fJ9rUzIMcZQ youtube.com/watch?v=fJ9rUzIMcZQ some text here youtube.com/watch?v=fJ9rUzIMcZQhttps://www.youtube.com/…";
– Varkoume
Nov 23 at 0:27
add a comment |
I'm trying to find an expression to keep only the first youtube link I find in $render variable.
$render="some text here https://www.youtube.com/watch?v=fJ9rUzIMcZQ https://www.youtube.com/watch?v=fJ9rUzIMcZQ some text here https://www.youtube.com/watch?v=fJ9rUzIMcZQhttps://www.youtube.com/watch?v=fJ9rUzIMcZQ";
$prefix = "https://www.youtube.com/watch?v=";
$index = strpos($render, $prefix) + strlen($prefix);
$youtube = substr($render, $index);
$youtube = strtok($youtube,' ');
$regex="@(https)://(www.)?youtube.com/watch?v=[w_-].* *@";
preg_match($string, $render, $matches, PREG_OFFSET_CAPTURE);
$render = preg_replace($regex, "", $render);
$render = substr_replace($render, $matches[0][0], $matches[0][1], 0);
echo $render;
What I get
https://www.youtube.com/watch?v=fJ9rUzIMcZQ ://www.youtube.com/watch?v=fJ9rUzIMcZQ
What I want to get
https://www.youtube.com/watch?v=fJ9rUzIMcZQ
P.S.
The last two links are combined
php regex hyperlink youtube
I'm trying to find an expression to keep only the first youtube link I find in $render variable.
$render="some text here https://www.youtube.com/watch?v=fJ9rUzIMcZQ https://www.youtube.com/watch?v=fJ9rUzIMcZQ some text here https://www.youtube.com/watch?v=fJ9rUzIMcZQhttps://www.youtube.com/watch?v=fJ9rUzIMcZQ";
$prefix = "https://www.youtube.com/watch?v=";
$index = strpos($render, $prefix) + strlen($prefix);
$youtube = substr($render, $index);
$youtube = strtok($youtube,' ');
$regex="@(https)://(www.)?youtube.com/watch?v=[w_-].* *@";
preg_match($string, $render, $matches, PREG_OFFSET_CAPTURE);
$render = preg_replace($regex, "", $render);
$render = substr_replace($render, $matches[0][0], $matches[0][1], 0);
echo $render;
What I get
https://www.youtube.com/watch?v=fJ9rUzIMcZQ ://www.youtube.com/watch?v=fJ9rUzIMcZQ
What I want to get
https://www.youtube.com/watch?v=fJ9rUzIMcZQ
P.S.
The last two links are combined
php regex hyperlink youtube
php regex hyperlink youtube
edited Nov 23 at 0:30
asked Nov 22 at 23:49
Varkoume
103
103
Is$rendera string filled with links, each link separated from the others by a space?
– Terminus
Nov 22 at 23:57
Yes. They are separated with space in except the last two. They are combined.
– Varkoume
Nov 23 at 0:01
1
And if you only want the first link, why not simplyexplode()into an array, grab the first element, and confirm that it is a youtube URL ? If not, grab next and repeat.
– ivanivan
Nov 23 at 0:09
Sorry for not describing correctly what I want. There's possibility of containing and other words except of links. Like: $render="some text here youtube.com/watch?v=fJ9rUzIMcZQ youtube.com/watch?v=fJ9rUzIMcZQ some text here youtube.com/watch?v=fJ9rUzIMcZQhttps://www.youtube.com/…";
– Varkoume
Nov 23 at 0:27
add a comment |
Is$rendera string filled with links, each link separated from the others by a space?
– Terminus
Nov 22 at 23:57
Yes. They are separated with space in except the last two. They are combined.
– Varkoume
Nov 23 at 0:01
1
And if you only want the first link, why not simplyexplode()into an array, grab the first element, and confirm that it is a youtube URL ? If not, grab next and repeat.
– ivanivan
Nov 23 at 0:09
Sorry for not describing correctly what I want. There's possibility of containing and other words except of links. Like: $render="some text here youtube.com/watch?v=fJ9rUzIMcZQ youtube.com/watch?v=fJ9rUzIMcZQ some text here youtube.com/watch?v=fJ9rUzIMcZQhttps://www.youtube.com/…";
– Varkoume
Nov 23 at 0:27
Is
$render a string filled with links, each link separated from the others by a space?– Terminus
Nov 22 at 23:57
Is
$render a string filled with links, each link separated from the others by a space?– Terminus
Nov 22 at 23:57
Yes. They are separated with space in except the last two. They are combined.
– Varkoume
Nov 23 at 0:01
Yes. They are separated with space in except the last two. They are combined.
– Varkoume
Nov 23 at 0:01
1
1
And if you only want the first link, why not simply
explode() into an array, grab the first element, and confirm that it is a youtube URL ? If not, grab next and repeat.– ivanivan
Nov 23 at 0:09
And if you only want the first link, why not simply
explode() into an array, grab the first element, and confirm that it is a youtube URL ? If not, grab next and repeat.– ivanivan
Nov 23 at 0:09
Sorry for not describing correctly what I want. There's possibility of containing and other words except of links. Like: $render="some text here youtube.com/watch?v=fJ9rUzIMcZQ youtube.com/watch?v=fJ9rUzIMcZQ some text here youtube.com/watch?v=fJ9rUzIMcZQhttps://www.youtube.com/…";
– Varkoume
Nov 23 at 0:27
Sorry for not describing correctly what I want. There's possibility of containing and other words except of links. Like: $render="some text here youtube.com/watch?v=fJ9rUzIMcZQ youtube.com/watch?v=fJ9rUzIMcZQ some text here youtube.com/watch?v=fJ9rUzIMcZQhttps://www.youtube.com/…";
– Varkoume
Nov 23 at 0:27
add a comment |
2 Answers
2
active
oldest
votes
Try limiting how much the regex can match, so it doesn't spill over into the next url:
(?:https://www.)?youtube.com/watch?v=[w_-]{1,11}
regex101 demo
$render = "some text here youtube.com/watch?v=fJ9rUzIMcZQ youtube.com/watch?v=fJ9rUzIMcZQ some text here youtube.com/watch?v=fJ9rUzIMcZQhttps://www.youtube.com/...";
preg_match('/(?:https://www.)?youtube.com/watch?v=[w_-]{1,11}/', $render, $matches);
$render = $matches[0];
echo $render; // => youtube.com/watch?v=fJ9rUzIMcZQ
3v41.org demo
No problem @Varkoume, happy to help :) When you have some time, please be sure to check out the StackOverflow tour.
– Davіd
Nov 23 at 0:33
add a comment |
Using numeric delimiters is not-so-future proof in my opinion, this could work as well:
(https)://(www.)?youtube.com/watch?v=[w-].*?(?=(s|b|https?))
The positive lookahead "(?=(s|b|https?))" will match (but not include) a delimiter whitespace or word bound, furthermore it will recognize the beginning of a new URL with http(s) and will not match it, the lazy loading will match less characters up to the end of the link.
I also changed the set because "w" already includes the underscore.
If tomorrow YT decides to make URLs that are 24 characters you'll be fine anyways, until the latter part still remains included in the set.
This cover all cases of space, newline and even recognize the two URLs that are attached.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439183%2fphp-regular-expression-to-find-the-first-youtube-link%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try limiting how much the regex can match, so it doesn't spill over into the next url:
(?:https://www.)?youtube.com/watch?v=[w_-]{1,11}
regex101 demo
$render = "some text here youtube.com/watch?v=fJ9rUzIMcZQ youtube.com/watch?v=fJ9rUzIMcZQ some text here youtube.com/watch?v=fJ9rUzIMcZQhttps://www.youtube.com/...";
preg_match('/(?:https://www.)?youtube.com/watch?v=[w_-]{1,11}/', $render, $matches);
$render = $matches[0];
echo $render; // => youtube.com/watch?v=fJ9rUzIMcZQ
3v41.org demo
No problem @Varkoume, happy to help :) When you have some time, please be sure to check out the StackOverflow tour.
– Davіd
Nov 23 at 0:33
add a comment |
Try limiting how much the regex can match, so it doesn't spill over into the next url:
(?:https://www.)?youtube.com/watch?v=[w_-]{1,11}
regex101 demo
$render = "some text here youtube.com/watch?v=fJ9rUzIMcZQ youtube.com/watch?v=fJ9rUzIMcZQ some text here youtube.com/watch?v=fJ9rUzIMcZQhttps://www.youtube.com/...";
preg_match('/(?:https://www.)?youtube.com/watch?v=[w_-]{1,11}/', $render, $matches);
$render = $matches[0];
echo $render; // => youtube.com/watch?v=fJ9rUzIMcZQ
3v41.org demo
No problem @Varkoume, happy to help :) When you have some time, please be sure to check out the StackOverflow tour.
– Davіd
Nov 23 at 0:33
add a comment |
Try limiting how much the regex can match, so it doesn't spill over into the next url:
(?:https://www.)?youtube.com/watch?v=[w_-]{1,11}
regex101 demo
$render = "some text here youtube.com/watch?v=fJ9rUzIMcZQ youtube.com/watch?v=fJ9rUzIMcZQ some text here youtube.com/watch?v=fJ9rUzIMcZQhttps://www.youtube.com/...";
preg_match('/(?:https://www.)?youtube.com/watch?v=[w_-]{1,11}/', $render, $matches);
$render = $matches[0];
echo $render; // => youtube.com/watch?v=fJ9rUzIMcZQ
3v41.org demo
Try limiting how much the regex can match, so it doesn't spill over into the next url:
(?:https://www.)?youtube.com/watch?v=[w_-]{1,11}
regex101 demo
$render = "some text here youtube.com/watch?v=fJ9rUzIMcZQ youtube.com/watch?v=fJ9rUzIMcZQ some text here youtube.com/watch?v=fJ9rUzIMcZQhttps://www.youtube.com/...";
preg_match('/(?:https://www.)?youtube.com/watch?v=[w_-]{1,11}/', $render, $matches);
$render = $matches[0];
echo $render; // => youtube.com/watch?v=fJ9rUzIMcZQ
3v41.org demo
answered Nov 23 at 0:26
Davіd
3,62541635
3,62541635
No problem @Varkoume, happy to help :) When you have some time, please be sure to check out the StackOverflow tour.
– Davіd
Nov 23 at 0:33
add a comment |
No problem @Varkoume, happy to help :) When you have some time, please be sure to check out the StackOverflow tour.
– Davіd
Nov 23 at 0:33
No problem @Varkoume, happy to help :) When you have some time, please be sure to check out the StackOverflow tour.
– Davіd
Nov 23 at 0:33
No problem @Varkoume, happy to help :) When you have some time, please be sure to check out the StackOverflow tour.
– Davіd
Nov 23 at 0:33
add a comment |
Using numeric delimiters is not-so-future proof in my opinion, this could work as well:
(https)://(www.)?youtube.com/watch?v=[w-].*?(?=(s|b|https?))
The positive lookahead "(?=(s|b|https?))" will match (but not include) a delimiter whitespace or word bound, furthermore it will recognize the beginning of a new URL with http(s) and will not match it, the lazy loading will match less characters up to the end of the link.
I also changed the set because "w" already includes the underscore.
If tomorrow YT decides to make URLs that are 24 characters you'll be fine anyways, until the latter part still remains included in the set.
This cover all cases of space, newline and even recognize the two URLs that are attached.
add a comment |
Using numeric delimiters is not-so-future proof in my opinion, this could work as well:
(https)://(www.)?youtube.com/watch?v=[w-].*?(?=(s|b|https?))
The positive lookahead "(?=(s|b|https?))" will match (but not include) a delimiter whitespace or word bound, furthermore it will recognize the beginning of a new URL with http(s) and will not match it, the lazy loading will match less characters up to the end of the link.
I also changed the set because "w" already includes the underscore.
If tomorrow YT decides to make URLs that are 24 characters you'll be fine anyways, until the latter part still remains included in the set.
This cover all cases of space, newline and even recognize the two URLs that are attached.
add a comment |
Using numeric delimiters is not-so-future proof in my opinion, this could work as well:
(https)://(www.)?youtube.com/watch?v=[w-].*?(?=(s|b|https?))
The positive lookahead "(?=(s|b|https?))" will match (but not include) a delimiter whitespace or word bound, furthermore it will recognize the beginning of a new URL with http(s) and will not match it, the lazy loading will match less characters up to the end of the link.
I also changed the set because "w" already includes the underscore.
If tomorrow YT decides to make URLs that are 24 characters you'll be fine anyways, until the latter part still remains included in the set.
This cover all cases of space, newline and even recognize the two URLs that are attached.
Using numeric delimiters is not-so-future proof in my opinion, this could work as well:
(https)://(www.)?youtube.com/watch?v=[w-].*?(?=(s|b|https?))
The positive lookahead "(?=(s|b|https?))" will match (but not include) a delimiter whitespace or word bound, furthermore it will recognize the beginning of a new URL with http(s) and will not match it, the lazy loading will match less characters up to the end of the link.
I also changed the set because "w" already includes the underscore.
If tomorrow YT decides to make URLs that are 24 characters you'll be fine anyways, until the latter part still remains included in the set.
This cover all cases of space, newline and even recognize the two URLs that are attached.
answered Nov 23 at 2:06
Gigi
956
956
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439183%2fphp-regular-expression-to-find-the-first-youtube-link%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is
$rendera string filled with links, each link separated from the others by a space?– Terminus
Nov 22 at 23:57
Yes. They are separated with space in except the last two. They are combined.
– Varkoume
Nov 23 at 0:01
1
And if you only want the first link, why not simply
explode()into an array, grab the first element, and confirm that it is a youtube URL ? If not, grab next and repeat.– ivanivan
Nov 23 at 0:09
Sorry for not describing correctly what I want. There's possibility of containing and other words except of links. Like: $render="some text here youtube.com/watch?v=fJ9rUzIMcZQ youtube.com/watch?v=fJ9rUzIMcZQ some text here youtube.com/watch?v=fJ9rUzIMcZQhttps://www.youtube.com/…";
– Varkoume
Nov 23 at 0:27