How to isolate specific strings (url and 10 characters after) from a text and add them to an array with PHP?












0















If I have the following text:




Lorem ipsum dolor sit amet, https://example.com/1aDb_sc-Xy consectetur
adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat https://example.com/h3ab6--sc3 nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.




How can I extract the URL-s from it with PHP? I know that the urls are starting with https://example.com/ and than 10 character are following it.



My goal is to extract an array that will look like this with PHP:



$array = 
[
'https://example.com/1aDb_sc-Xy',
'https://example.com/h3ab6--sc3'
];









share|improve this question

























  • Try preg_match_all('~bhttps://example.com/.{10}~', $str, $matches);

    – The fourth bird
    Nov 21 '18 at 8:54
















0















If I have the following text:




Lorem ipsum dolor sit amet, https://example.com/1aDb_sc-Xy consectetur
adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat https://example.com/h3ab6--sc3 nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.




How can I extract the URL-s from it with PHP? I know that the urls are starting with https://example.com/ and than 10 character are following it.



My goal is to extract an array that will look like this with PHP:



$array = 
[
'https://example.com/1aDb_sc-Xy',
'https://example.com/h3ab6--sc3'
];









share|improve this question

























  • Try preg_match_all('~bhttps://example.com/.{10}~', $str, $matches);

    – The fourth bird
    Nov 21 '18 at 8:54














0












0








0


0






If I have the following text:




Lorem ipsum dolor sit amet, https://example.com/1aDb_sc-Xy consectetur
adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat https://example.com/h3ab6--sc3 nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.




How can I extract the URL-s from it with PHP? I know that the urls are starting with https://example.com/ and than 10 character are following it.



My goal is to extract an array that will look like this with PHP:



$array = 
[
'https://example.com/1aDb_sc-Xy',
'https://example.com/h3ab6--sc3'
];









share|improve this question
















If I have the following text:




Lorem ipsum dolor sit amet, https://example.com/1aDb_sc-Xy consectetur
adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat https://example.com/h3ab6--sc3 nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.




How can I extract the URL-s from it with PHP? I know that the urls are starting with https://example.com/ and than 10 character are following it.



My goal is to extract an array that will look like this with PHP:



$array = 
[
'https://example.com/1aDb_sc-Xy',
'https://example.com/h3ab6--sc3'
];






php regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 22:06









Sean Bright

93k14113128




93k14113128










asked Nov 20 '18 at 22:04









Emmanuel-AbEmmanuel-Ab

327




327













  • Try preg_match_all('~bhttps://example.com/.{10}~', $str, $matches);

    – The fourth bird
    Nov 21 '18 at 8:54



















  • Try preg_match_all('~bhttps://example.com/.{10}~', $str, $matches);

    – The fourth bird
    Nov 21 '18 at 8:54

















Try preg_match_all('~bhttps://example.com/.{10}~', $str, $matches);

– The fourth bird
Nov 21 '18 at 8:54





Try preg_match_all('~bhttps://example.com/.{10}~', $str, $matches);

– The fourth bird
Nov 21 '18 at 8:54












1 Answer
1






active

oldest

votes


















2














You can use preg_match_all with regex to search the string for https://example.com/, then up to the first white space after it:





<?php

$string = "Lorem ipsum dolor sit amet, https://example.com/1aDb_sc-Xy consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat https://example.com/h3ab6--sc3 nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

preg_match_all('!https://example.com/S+!', $string, $matches);

var_dump($matches[0]);

// array(2) {
// [0]=> string(30) "https://example.com/1aDb_sc-Xy"
// [1]=> string(30) "https://example.com/h3ab6--sc3"
// }




Edit:
To match https://example.com/ and then the next 10 characters exactly:



preg_match_all('!https://example.com/(.{10})!', $string, $matches);





share|improve this answer


























  • Your regex match also URL contain more than 10 characters. regex101.com/r/qiUCGK/1

    – Mohammad
    Nov 21 '18 at 3:30













  • Is there any way to take 10 characters after the url has been found, not to the first white space? Thank you for this in advance!!!

    – Emmanuel-Ab
    Nov 21 '18 at 6:27













  • @Emmanuel-Ab I've added the regex to match 10 characters above! :)

    – Gary Thomas
    Nov 21 '18 at 9:10











  • Thank you so much!!!

    – Emmanuel-Ab
    Nov 21 '18 at 9:53











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402275%2fhow-to-isolate-specific-strings-url-and-10-characters-after-from-a-text-and-ad%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can use preg_match_all with regex to search the string for https://example.com/, then up to the first white space after it:





<?php

$string = "Lorem ipsum dolor sit amet, https://example.com/1aDb_sc-Xy consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat https://example.com/h3ab6--sc3 nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

preg_match_all('!https://example.com/S+!', $string, $matches);

var_dump($matches[0]);

// array(2) {
// [0]=> string(30) "https://example.com/1aDb_sc-Xy"
// [1]=> string(30) "https://example.com/h3ab6--sc3"
// }




Edit:
To match https://example.com/ and then the next 10 characters exactly:



preg_match_all('!https://example.com/(.{10})!', $string, $matches);





share|improve this answer


























  • Your regex match also URL contain more than 10 characters. regex101.com/r/qiUCGK/1

    – Mohammad
    Nov 21 '18 at 3:30













  • Is there any way to take 10 characters after the url has been found, not to the first white space? Thank you for this in advance!!!

    – Emmanuel-Ab
    Nov 21 '18 at 6:27













  • @Emmanuel-Ab I've added the regex to match 10 characters above! :)

    – Gary Thomas
    Nov 21 '18 at 9:10











  • Thank you so much!!!

    – Emmanuel-Ab
    Nov 21 '18 at 9:53
















2














You can use preg_match_all with regex to search the string for https://example.com/, then up to the first white space after it:





<?php

$string = "Lorem ipsum dolor sit amet, https://example.com/1aDb_sc-Xy consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat https://example.com/h3ab6--sc3 nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

preg_match_all('!https://example.com/S+!', $string, $matches);

var_dump($matches[0]);

// array(2) {
// [0]=> string(30) "https://example.com/1aDb_sc-Xy"
// [1]=> string(30) "https://example.com/h3ab6--sc3"
// }




Edit:
To match https://example.com/ and then the next 10 characters exactly:



preg_match_all('!https://example.com/(.{10})!', $string, $matches);





share|improve this answer


























  • Your regex match also URL contain more than 10 characters. regex101.com/r/qiUCGK/1

    – Mohammad
    Nov 21 '18 at 3:30













  • Is there any way to take 10 characters after the url has been found, not to the first white space? Thank you for this in advance!!!

    – Emmanuel-Ab
    Nov 21 '18 at 6:27













  • @Emmanuel-Ab I've added the regex to match 10 characters above! :)

    – Gary Thomas
    Nov 21 '18 at 9:10











  • Thank you so much!!!

    – Emmanuel-Ab
    Nov 21 '18 at 9:53














2












2








2







You can use preg_match_all with regex to search the string for https://example.com/, then up to the first white space after it:





<?php

$string = "Lorem ipsum dolor sit amet, https://example.com/1aDb_sc-Xy consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat https://example.com/h3ab6--sc3 nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

preg_match_all('!https://example.com/S+!', $string, $matches);

var_dump($matches[0]);

// array(2) {
// [0]=> string(30) "https://example.com/1aDb_sc-Xy"
// [1]=> string(30) "https://example.com/h3ab6--sc3"
// }




Edit:
To match https://example.com/ and then the next 10 characters exactly:



preg_match_all('!https://example.com/(.{10})!', $string, $matches);





share|improve this answer















You can use preg_match_all with regex to search the string for https://example.com/, then up to the first white space after it:





<?php

$string = "Lorem ipsum dolor sit amet, https://example.com/1aDb_sc-Xy consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat https://example.com/h3ab6--sc3 nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

preg_match_all('!https://example.com/S+!', $string, $matches);

var_dump($matches[0]);

// array(2) {
// [0]=> string(30) "https://example.com/1aDb_sc-Xy"
// [1]=> string(30) "https://example.com/h3ab6--sc3"
// }




Edit:
To match https://example.com/ and then the next 10 characters exactly:



preg_match_all('!https://example.com/(.{10})!', $string, $matches);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 9:09

























answered Nov 20 '18 at 22:17









Gary ThomasGary Thomas

1,5151415




1,5151415













  • Your regex match also URL contain more than 10 characters. regex101.com/r/qiUCGK/1

    – Mohammad
    Nov 21 '18 at 3:30













  • Is there any way to take 10 characters after the url has been found, not to the first white space? Thank you for this in advance!!!

    – Emmanuel-Ab
    Nov 21 '18 at 6:27













  • @Emmanuel-Ab I've added the regex to match 10 characters above! :)

    – Gary Thomas
    Nov 21 '18 at 9:10











  • Thank you so much!!!

    – Emmanuel-Ab
    Nov 21 '18 at 9:53



















  • Your regex match also URL contain more than 10 characters. regex101.com/r/qiUCGK/1

    – Mohammad
    Nov 21 '18 at 3:30













  • Is there any way to take 10 characters after the url has been found, not to the first white space? Thank you for this in advance!!!

    – Emmanuel-Ab
    Nov 21 '18 at 6:27













  • @Emmanuel-Ab I've added the regex to match 10 characters above! :)

    – Gary Thomas
    Nov 21 '18 at 9:10











  • Thank you so much!!!

    – Emmanuel-Ab
    Nov 21 '18 at 9:53

















Your regex match also URL contain more than 10 characters. regex101.com/r/qiUCGK/1

– Mohammad
Nov 21 '18 at 3:30







Your regex match also URL contain more than 10 characters. regex101.com/r/qiUCGK/1

– Mohammad
Nov 21 '18 at 3:30















Is there any way to take 10 characters after the url has been found, not to the first white space? Thank you for this in advance!!!

– Emmanuel-Ab
Nov 21 '18 at 6:27







Is there any way to take 10 characters after the url has been found, not to the first white space? Thank you for this in advance!!!

– Emmanuel-Ab
Nov 21 '18 at 6:27















@Emmanuel-Ab I've added the regex to match 10 characters above! :)

– Gary Thomas
Nov 21 '18 at 9:10





@Emmanuel-Ab I've added the regex to match 10 characters above! :)

– Gary Thomas
Nov 21 '18 at 9:10













Thank you so much!!!

– Emmanuel-Ab
Nov 21 '18 at 9:53





Thank you so much!!!

– Emmanuel-Ab
Nov 21 '18 at 9:53




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402275%2fhow-to-isolate-specific-strings-url-and-10-characters-after-from-a-text-and-ad%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?