How to extract and format specific values from a stringConvert from MySQL datetime to another format with PHPHow do I get (extract) a file extension in PHP?How do I get a YouTube video thumbnail from the YouTube API?How to Sort Multi-dimensional Array by Value?How to check if a string starts with a specified string?How do I check if a string contains a specific word?Remove the last character from stringExtract numbers from a stringQuery on a piece of codeHow to upload file and rename if file is exist in folder while we keep original file name?

Is it possible to change an API response using the host file?

A famous scholar sent me an unpublished draft of hers. Then she died. I think her work should be published. What should I do?

Garage door sticks on a bolt

Beyond Futuristic Technology for an Alien Warship?

What should I consider when deciding whether to delay an exam?

Knights and Knaves: What does C say?

What is this end portal thingy?

An impressive body of work

Concerning a relationship in the team

Which altitudes are safest for VFR?

Can an energy drink or chocolate before an exam be useful ? What sort of other edible goods be helpful?

what organs or modifications would be needed to have hairy fish?

Do interval ratios take overtones into account or solely the fundamental frequency?

Why aren't faces sharp in my f/1.8 portraits even though I'm carefully using center-point autofocus?

Is the illusion created by Invoke Duplicity affected by difficult terrain?

I reverse the source code, you reverse the input!

Delete n lines skip 1 line script

Where to find the Arxiv endorsement code?

What can Thomas Cook customers who have not yet departed do now it has stopped operating?

Convert a string of digits from words to an integer

Can a passenger predict that an airline is about to go bankrupt?

If a spaceship ran out of fuel somewhere in space between Earth and Mars, does it slowly drift off to the Sun?

Is there a list of world wide upcoming space events on the web?

What does it mean by "my days-of-the-week underwear only go to Thursday" in this context?



How to extract and format specific values from a string


Convert from MySQL datetime to another format with PHPHow do I get (extract) a file extension in PHP?How do I get a YouTube video thumbnail from the YouTube API?How to Sort Multi-dimensional Array by Value?How to check if a string starts with a specified string?How do I check if a string contains a specific word?Remove the last character from stringExtract numbers from a stringQuery on a piece of codeHow to upload file and rename if file is exist in folder while we keep original file name?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















We have files uploaded to our server in a specific format that indicates a version and build date, ex: 5_5_61__20180828.dav



The first group of numbers represents the version 5_5_61, and the last group of numbers represents the build date 20180828 and then .dav is just the file extension.



How would we be able to extract and format this file name to appear like this:



Version: 5.5.61 Build Date: 08/28/2018


I've been trying different variations of this snippet of code but can't seem to figure out the proper method:



$file = '5_5_61__20180828.dav';
echo substr($file, strpos($file , "_") + 1);


I should note that I am willing to change the file names if there's an easier way to achieve the final result using a different naming convention for the files.










share|improve this question


























  • If you're sure the filename will always be in the same format, i would use a regular expression. /(d+)_(d+)_(d+)__(d4)(d2)(d2)/ php.net/manual/en/function.preg-match.php

    – bassxzero
    Mar 28 at 19:25


















1















We have files uploaded to our server in a specific format that indicates a version and build date, ex: 5_5_61__20180828.dav



The first group of numbers represents the version 5_5_61, and the last group of numbers represents the build date 20180828 and then .dav is just the file extension.



How would we be able to extract and format this file name to appear like this:



Version: 5.5.61 Build Date: 08/28/2018


I've been trying different variations of this snippet of code but can't seem to figure out the proper method:



$file = '5_5_61__20180828.dav';
echo substr($file, strpos($file , "_") + 1);


I should note that I am willing to change the file names if there's an easier way to achieve the final result using a different naming convention for the files.










share|improve this question


























  • If you're sure the filename will always be in the same format, i would use a regular expression. /(d+)_(d+)_(d+)__(d4)(d2)(d2)/ php.net/manual/en/function.preg-match.php

    – bassxzero
    Mar 28 at 19:25














1












1








1








We have files uploaded to our server in a specific format that indicates a version and build date, ex: 5_5_61__20180828.dav



The first group of numbers represents the version 5_5_61, and the last group of numbers represents the build date 20180828 and then .dav is just the file extension.



How would we be able to extract and format this file name to appear like this:



Version: 5.5.61 Build Date: 08/28/2018


I've been trying different variations of this snippet of code but can't seem to figure out the proper method:



$file = '5_5_61__20180828.dav';
echo substr($file, strpos($file , "_") + 1);


I should note that I am willing to change the file names if there's an easier way to achieve the final result using a different naming convention for the files.










share|improve this question
















We have files uploaded to our server in a specific format that indicates a version and build date, ex: 5_5_61__20180828.dav



The first group of numbers represents the version 5_5_61, and the last group of numbers represents the build date 20180828 and then .dav is just the file extension.



How would we be able to extract and format this file name to appear like this:



Version: 5.5.61 Build Date: 08/28/2018


I've been trying different variations of this snippet of code but can't seem to figure out the proper method:



$file = '5_5_61__20180828.dav';
echo substr($file, strpos($file , "_") + 1);


I should note that I am willing to change the file names if there's an easier way to achieve the final result using a different naming convention for the files.







php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 19:31









Qirel

17.4k7 gold badges28 silver badges46 bronze badges




17.4k7 gold badges28 silver badges46 bronze badges










asked Mar 28 at 19:22









AJ47AJ47

2665 silver badges21 bronze badges




2665 silver badges21 bronze badges















  • If you're sure the filename will always be in the same format, i would use a regular expression. /(d+)_(d+)_(d+)__(d4)(d2)(d2)/ php.net/manual/en/function.preg-match.php

    – bassxzero
    Mar 28 at 19:25


















  • If you're sure the filename will always be in the same format, i would use a regular expression. /(d+)_(d+)_(d+)__(d4)(d2)(d2)/ php.net/manual/en/function.preg-match.php

    – bassxzero
    Mar 28 at 19:25

















If you're sure the filename will always be in the same format, i would use a regular expression. /(d+)_(d+)_(d+)__(d4)(d2)(d2)/ php.net/manual/en/function.preg-match.php

– bassxzero
Mar 28 at 19:25






If you're sure the filename will always be in the same format, i would use a regular expression. /(d+)_(d+)_(d+)__(d4)(d2)(d2)/ php.net/manual/en/function.preg-match.php

– bassxzero
Mar 28 at 19:25













2 Answers
2






active

oldest

votes


















3
















If you know that the versions and dates are separated by __ (two underscores), it's quite straight forward.



First split the string on __ (two underscores) - that will give you the version, and the date with extension. Explode the last part again to separate date from extension. Then replace _ (one underscore) with . on your version, and print it all.



Since 20180828 isn't a standard date-format for strtotime() functions, use DateTime::createFromFormat() to create a date-object.



$filename = '5_5_61__20180828.dav';
$parts = explode("__", $filename);
$parts2 = explode(".", $parts[1]);

$version = str_replce("_", ".", $parts[0]);
$date = $parts2[0];
$extension = $parts2[1];

$date = DateTime::createFromFormat("Ymd", $date)->format("m/d/Y");

echo "Version: $version. Build Date: $date";


  • Live demo at https://3v4l.org/4Lk2T





share|improve this answer




















  • 1





    The second explode (.) can be replaced with a substr since the date is fixed in length

    – Andreas
    Mar 28 at 19:33











  • Fair point, that'll be a little more efficient too. Can probably reduce this whole split ordeal into a regex as well.

    – Qirel
    Mar 28 at 19:34











  • Yes, regex is possible, but not sure it will be faster. (But then again, I don't think this code will run thousands of times). But I just commented since it may make the code slightly cleaner to add the substr in the DateTime line than a separate line. All in all, +1

    – Andreas
    Mar 28 at 19:36











  • Thank you, exactly what I was looking for!

    – AJ47
    Mar 28 at 19:48






  • 1





    Or just $parts = explode("__", pathinfo($filename, PATHINFO_FILENAME));

    – AbraCadaver
    Mar 28 at 20:22


















1
















https://ideone.com/a5Vudv



$file = '5_5_61__20180828.dav';
$version = str_replace("_", ".", substr($file, 0, -14));
$d = DateTime::createFromFormat("Ymd", substr($file, -12, 8))->format("m/d/Y");
echo 'Version: ' . $version . ' Build Date: '. $d;





share|improve this answer



























    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/4.0/"u003ecc by-sa 4.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%2f55405415%2fhow-to-extract-and-format-specific-values-from-a-string%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









    3
















    If you know that the versions and dates are separated by __ (two underscores), it's quite straight forward.



    First split the string on __ (two underscores) - that will give you the version, and the date with extension. Explode the last part again to separate date from extension. Then replace _ (one underscore) with . on your version, and print it all.



    Since 20180828 isn't a standard date-format for strtotime() functions, use DateTime::createFromFormat() to create a date-object.



    $filename = '5_5_61__20180828.dav';
    $parts = explode("__", $filename);
    $parts2 = explode(".", $parts[1]);

    $version = str_replce("_", ".", $parts[0]);
    $date = $parts2[0];
    $extension = $parts2[1];

    $date = DateTime::createFromFormat("Ymd", $date)->format("m/d/Y");

    echo "Version: $version. Build Date: $date";


    • Live demo at https://3v4l.org/4Lk2T





    share|improve this answer




















    • 1





      The second explode (.) can be replaced with a substr since the date is fixed in length

      – Andreas
      Mar 28 at 19:33











    • Fair point, that'll be a little more efficient too. Can probably reduce this whole split ordeal into a regex as well.

      – Qirel
      Mar 28 at 19:34











    • Yes, regex is possible, but not sure it will be faster. (But then again, I don't think this code will run thousands of times). But I just commented since it may make the code slightly cleaner to add the substr in the DateTime line than a separate line. All in all, +1

      – Andreas
      Mar 28 at 19:36











    • Thank you, exactly what I was looking for!

      – AJ47
      Mar 28 at 19:48






    • 1





      Or just $parts = explode("__", pathinfo($filename, PATHINFO_FILENAME));

      – AbraCadaver
      Mar 28 at 20:22















    3
















    If you know that the versions and dates are separated by __ (two underscores), it's quite straight forward.



    First split the string on __ (two underscores) - that will give you the version, and the date with extension. Explode the last part again to separate date from extension. Then replace _ (one underscore) with . on your version, and print it all.



    Since 20180828 isn't a standard date-format for strtotime() functions, use DateTime::createFromFormat() to create a date-object.



    $filename = '5_5_61__20180828.dav';
    $parts = explode("__", $filename);
    $parts2 = explode(".", $parts[1]);

    $version = str_replce("_", ".", $parts[0]);
    $date = $parts2[0];
    $extension = $parts2[1];

    $date = DateTime::createFromFormat("Ymd", $date)->format("m/d/Y");

    echo "Version: $version. Build Date: $date";


    • Live demo at https://3v4l.org/4Lk2T





    share|improve this answer




















    • 1





      The second explode (.) can be replaced with a substr since the date is fixed in length

      – Andreas
      Mar 28 at 19:33











    • Fair point, that'll be a little more efficient too. Can probably reduce this whole split ordeal into a regex as well.

      – Qirel
      Mar 28 at 19:34











    • Yes, regex is possible, but not sure it will be faster. (But then again, I don't think this code will run thousands of times). But I just commented since it may make the code slightly cleaner to add the substr in the DateTime line than a separate line. All in all, +1

      – Andreas
      Mar 28 at 19:36











    • Thank you, exactly what I was looking for!

      – AJ47
      Mar 28 at 19:48






    • 1





      Or just $parts = explode("__", pathinfo($filename, PATHINFO_FILENAME));

      – AbraCadaver
      Mar 28 at 20:22













    3














    3










    3









    If you know that the versions and dates are separated by __ (two underscores), it's quite straight forward.



    First split the string on __ (two underscores) - that will give you the version, and the date with extension. Explode the last part again to separate date from extension. Then replace _ (one underscore) with . on your version, and print it all.



    Since 20180828 isn't a standard date-format for strtotime() functions, use DateTime::createFromFormat() to create a date-object.



    $filename = '5_5_61__20180828.dav';
    $parts = explode("__", $filename);
    $parts2 = explode(".", $parts[1]);

    $version = str_replce("_", ".", $parts[0]);
    $date = $parts2[0];
    $extension = $parts2[1];

    $date = DateTime::createFromFormat("Ymd", $date)->format("m/d/Y");

    echo "Version: $version. Build Date: $date";


    • Live demo at https://3v4l.org/4Lk2T





    share|improve this answer













    If you know that the versions and dates are separated by __ (two underscores), it's quite straight forward.



    First split the string on __ (two underscores) - that will give you the version, and the date with extension. Explode the last part again to separate date from extension. Then replace _ (one underscore) with . on your version, and print it all.



    Since 20180828 isn't a standard date-format for strtotime() functions, use DateTime::createFromFormat() to create a date-object.



    $filename = '5_5_61__20180828.dav';
    $parts = explode("__", $filename);
    $parts2 = explode(".", $parts[1]);

    $version = str_replce("_", ".", $parts[0]);
    $date = $parts2[0];
    $extension = $parts2[1];

    $date = DateTime::createFromFormat("Ymd", $date)->format("m/d/Y");

    echo "Version: $version. Build Date: $date";


    • Live demo at https://3v4l.org/4Lk2T






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 28 at 19:28









    QirelQirel

    17.4k7 gold badges28 silver badges46 bronze badges




    17.4k7 gold badges28 silver badges46 bronze badges










    • 1





      The second explode (.) can be replaced with a substr since the date is fixed in length

      – Andreas
      Mar 28 at 19:33











    • Fair point, that'll be a little more efficient too. Can probably reduce this whole split ordeal into a regex as well.

      – Qirel
      Mar 28 at 19:34











    • Yes, regex is possible, but not sure it will be faster. (But then again, I don't think this code will run thousands of times). But I just commented since it may make the code slightly cleaner to add the substr in the DateTime line than a separate line. All in all, +1

      – Andreas
      Mar 28 at 19:36











    • Thank you, exactly what I was looking for!

      – AJ47
      Mar 28 at 19:48






    • 1





      Or just $parts = explode("__", pathinfo($filename, PATHINFO_FILENAME));

      – AbraCadaver
      Mar 28 at 20:22












    • 1





      The second explode (.) can be replaced with a substr since the date is fixed in length

      – Andreas
      Mar 28 at 19:33











    • Fair point, that'll be a little more efficient too. Can probably reduce this whole split ordeal into a regex as well.

      – Qirel
      Mar 28 at 19:34











    • Yes, regex is possible, but not sure it will be faster. (But then again, I don't think this code will run thousands of times). But I just commented since it may make the code slightly cleaner to add the substr in the DateTime line than a separate line. All in all, +1

      – Andreas
      Mar 28 at 19:36











    • Thank you, exactly what I was looking for!

      – AJ47
      Mar 28 at 19:48






    • 1





      Or just $parts = explode("__", pathinfo($filename, PATHINFO_FILENAME));

      – AbraCadaver
      Mar 28 at 20:22







    1




    1





    The second explode (.) can be replaced with a substr since the date is fixed in length

    – Andreas
    Mar 28 at 19:33





    The second explode (.) can be replaced with a substr since the date is fixed in length

    – Andreas
    Mar 28 at 19:33













    Fair point, that'll be a little more efficient too. Can probably reduce this whole split ordeal into a regex as well.

    – Qirel
    Mar 28 at 19:34





    Fair point, that'll be a little more efficient too. Can probably reduce this whole split ordeal into a regex as well.

    – Qirel
    Mar 28 at 19:34













    Yes, regex is possible, but not sure it will be faster. (But then again, I don't think this code will run thousands of times). But I just commented since it may make the code slightly cleaner to add the substr in the DateTime line than a separate line. All in all, +1

    – Andreas
    Mar 28 at 19:36





    Yes, regex is possible, but not sure it will be faster. (But then again, I don't think this code will run thousands of times). But I just commented since it may make the code slightly cleaner to add the substr in the DateTime line than a separate line. All in all, +1

    – Andreas
    Mar 28 at 19:36













    Thank you, exactly what I was looking for!

    – AJ47
    Mar 28 at 19:48





    Thank you, exactly what I was looking for!

    – AJ47
    Mar 28 at 19:48




    1




    1





    Or just $parts = explode("__", pathinfo($filename, PATHINFO_FILENAME));

    – AbraCadaver
    Mar 28 at 20:22





    Or just $parts = explode("__", pathinfo($filename, PATHINFO_FILENAME));

    – AbraCadaver
    Mar 28 at 20:22













    1
















    https://ideone.com/a5Vudv



    $file = '5_5_61__20180828.dav';
    $version = str_replace("_", ".", substr($file, 0, -14));
    $d = DateTime::createFromFormat("Ymd", substr($file, -12, 8))->format("m/d/Y");
    echo 'Version: ' . $version . ' Build Date: '. $d;





    share|improve this answer





























      1
















      https://ideone.com/a5Vudv



      $file = '5_5_61__20180828.dav';
      $version = str_replace("_", ".", substr($file, 0, -14));
      $d = DateTime::createFromFormat("Ymd", substr($file, -12, 8))->format("m/d/Y");
      echo 'Version: ' . $version . ' Build Date: '. $d;





      share|improve this answer



























        1














        1










        1









        https://ideone.com/a5Vudv



        $file = '5_5_61__20180828.dav';
        $version = str_replace("_", ".", substr($file, 0, -14));
        $d = DateTime::createFromFormat("Ymd", substr($file, -12, 8))->format("m/d/Y");
        echo 'Version: ' . $version . ' Build Date: '. $d;





        share|improve this answer













        https://ideone.com/a5Vudv



        $file = '5_5_61__20180828.dav';
        $version = str_replace("_", ".", substr($file, 0, -14));
        $d = DateTime::createFromFormat("Ymd", substr($file, -12, 8))->format("m/d/Y");
        echo 'Version: ' . $version . ' Build Date: '. $d;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 28 at 19:42









        AlexAlex

        15.5k1 gold badge20 silver badges39 bronze badges




        15.5k1 gold badge20 silver badges39 bronze badges































            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%2f55405415%2fhow-to-extract-and-format-specific-values-from-a-string%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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript