Professional Web Applications Themes

Split string like 'something.jpg,123' into $filename and $number - PHP Development

I am trying to take a string and split it into a filename and a number where the number is what follows the *last* instance of a comma in that string. Split and explode won't work because if the filename part of the original string contains a , then I end up with too many parts. if (!preg_match ('/^(.*),([0-9]+)$/', $string, $matches)) {throwError ();} also fails, because the first part of the regular expression becomes greedy if there is a , in it. I.e. can anyone suggest a solution such that $string = 'something.jpg,123'; returns: $matches['filename'] = something.jpg $matches['number'] = 123 ...

  1. #1

    Default Split string like 'something.jpg,123' into $filename and $number



    I am trying to take a string and split it into a filename and a number
    where the number is what follows the *last* instance of a comma in that
    string.

    Split and explode won't work because if the filename part of the original
    string contains a , then I end up with too many parts.

    if (!preg_match ('/^(.*),([0-9]+)$/', $string, $matches)) {throwError ();}
    also fails, because the first part of the regular expression becomes
    greedy if there is a , in it.


    I.e. can anyone suggest a solution such that

    $string = 'something.jpg,123';
    returns:
    $matches['filename'] = something.jpg
    $matches['number'] = 123


    $string = 'something,foo.jpg,123';
    returns:
    $matches['filename'] = something,foo.jpg
    $matches['number'] = 123


    and ideally

    $string = 'somethingfoo.jpg123'; // No comma contained so it's invalid
    returns:
    false

    ?


    Martin Lucas-Smith [url]www.geog.cam.ac.uk/~mvl22[/url]
    [url]www.lucas-smith.co.uk[/url]

    Martin Lucas-Smith Guest

  2. #2

    Default Re: Split string like 'something.jpg,123' into $filename and $number

    Hi

    I suggest you need something like this, in the PHP Manual

    =========================
    strrpos ( string haystack, char needle)


    Returns the numeric position of the last occurrence of needle in the
    haystack string. Note that the needle in this case can only be a single
    character. If a string is passed as the needle, then only the first
    character of that string will be used.

    If needle is not found, returns FALSE.

    =========================

    Once you've got the position of the last comma, you can use substr to pick
    the portion of the string to the left and right of it.



    "Martin Lucas-Smith" <mvl22cam.ac.uk> wrote in message
    news:Pine.SOL.4.44.0312051537230.17830-100000orange.csi.cam.ac.uk...
    >
    >
    > I am trying to take a string and split it into a filename and a number
    > where the number is what follows the *last* instance of a comma in that
    > string.
    >
    > Split and explode won't work because if the filename part of the original
    > string contains a , then I end up with too many parts.
    >
    > if (!preg_match ('/^(.*),([0-9]+)$/', $string, $matches)) {throwError ();}
    > also fails, because the first part of the regular expression becomes
    > greedy if there is a , in it.
    >
    >
    > I.e. can anyone suggest a solution such that
    >
    > $string = 'something.jpg,123';
    > returns:
    > $matches['filename'] = something.jpg
    > $matches['number'] = 123
    >
    >
    > $string = 'something,foo.jpg,123';
    > returns:
    > $matches['filename'] = something,foo.jpg
    > $matches['number'] = 123
    >
    >
    > and ideally
    >
    > $string = 'somethingfoo.jpg123'; // No comma contained so it's invalid
    > returns:
    > false
    >
    > ?
    >
    >
    > Martin Lucas-Smith [url]www.geog.cam.ac.uk/~mvl22[/url]
    > [url]www.lucas-smith.co.uk[/url]
    >

    Doc Dunning Guest

  3. #3

    Default Re: Split string like 'something.jpg,123' into $filename and $number

    "Martin Lucas-Smith" <mvl22cam.ac.uk> schrieb im Newsbeitrag
    news:Pine.SOL.4.44.0312051537230.17830-100000orange.csi.cam.ac.uk...
    >
    >
    > I am trying to take a string and split it into a filename and a number
    > where the number is what follows the *last* instance of a comma in that
    > string.
    >
    > Split and explode won't work because if the filename part of the original
    > string contains a , then I end up with too many parts.
    >
    > if (!preg_match ('/^(.*),([0-9]+)$/', $string, $matches)) {throwError ();}
    > also fails, because the first part of the regular expression becomes
    > greedy if there is a , in it.
    >
    >
    > I.e. can anyone suggest a solution such that
    >
    > $string = 'something.jpg,123';
    > returns:
    > $matches['filename'] = something.jpg
    > $matches['number'] = 123
    >
    >
    > $string = 'something,foo.jpg,123';
    > returns:
    > $matches['filename'] = something,foo.jpg
    > $matches['number'] = 123
    >
    >
    > and ideally
    >
    > $string = 'somethingfoo.jpg123'; // No comma contained so it's invalid
    > returns:
    > false
    >
    > ?
    $array = explode(",",$string);
    $c = count($array);
    if($c<2) $matches = false;
    else {
    $matches['filename'] = "";
    for($i=0;$i<$c;$i++) {
    if($i==$c-1) $matches['number'] = $array[$i];
    else $matches['filename'] .= $array[$i].",";
    }
    $matches['filename'] = substr($matches['filename'], 0, -1);
    }

    HTH
    Markus


    Markus Ernst Guest

  4. #4

    Default Re: Split string like 'something.jpg,123' into $filename and $number

    Martin Lucas-Smith <ac.uk> wrote in message news:<csi.cam.ac.uk>... 

    /^(\w+,?\w+\.jpg),(\d+)$/

    Done with "The Regex Coach" <http://www.weitz.de/regex-coach/>

    --
    "Silence is the only right answer for many wrong questions" --
    G.K.Moopanar, Politician
    Email: rrjanbiah-at-Y!com
    R. Guest

Similar Threads

  1. Split string?
    By Steve Grosz in forum Macromedia ColdFusion
    Replies: 1
    Last Post: June 13th, 10:21 PM
  2. Split string with nulls
    By siv786 in forum Macromedia ColdFusion
    Replies: 1
    Last Post: May 3rd, 05:14 PM
  3. split filename
    By 36US webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 1
    Last Post: January 26th, 08:19 PM
  4. new warning in 1.8 from String#split
    By Phil Tomson in forum Ruby
    Replies: 0
    Last Post: October 11th, 06:14 PM
  5. split a tab delimited string
    By Ajit in forum ASP.NET General
    Replies: 6
    Last Post: July 29th, 03:32 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139