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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts

  • R. Rajesh Jeba Anbiah
    Guest replied
    Re: Split string like 'something.jpg, 123' into $filename and $number

    Martin Lucas-Smith <mvl22@cam.ac.u k> wrote in message news:<Pine.SOL. 4.44.0312051537 230.17830-100000@orange.c si.cam.ac.uk>.. .[color=blue]
    > 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.j pg
    > $matches['number'] = 123
    >
    >
    > and ideally
    >
    > $string = 'somethingfoo.j pg123'; // No comma contained so it's invalid
    > returns:
    > false[/color]

    /^(\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

    Leave a comment:


  • Markus Ernst
    Guest replied
    Re: Split string like 'something.jpg, 123' into $filename and $number

    "Martin Lucas-Smith" <mvl22@cam.ac.u k> schrieb im Newsbeitrag
    news:Pine.SOL.4 .44.03120515372 30.17830-100000@orange.c si.cam.ac.uk...[color=blue]
    >
    >
    > 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.j pg
    > $matches['number'] = 123
    >
    >
    > and ideally
    >
    > $string = 'somethingfoo.j pg123'; // No comma contained so it's invalid
    > returns:
    > false
    >
    > ?[/color]

    $array = explode(",",$st ring);
    $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


    Leave a comment:


  • Doc Dunning
    Guest replied
    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" <mvl22@cam.ac.u k> wrote in message
    news:Pine.SOL.4 .44.03120515372 30.17830-100000@orange.c si.cam.ac.uk...[color=blue]
    >
    >
    > 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.j pg
    > $matches['number'] = 123
    >
    >
    > and ideally
    >
    > $string = 'somethingfoo.j pg123'; // No comma contained so it's invalid
    > returns:
    > false
    >
    > ?
    >
    >
    > Martin Lucas-Smith www.geog.cam.ac.uk/~mvl22
    > www.lucas-smith.co.uk
    >[/color]


    Leave a comment:


  • Martin Lucas-Smith
    Guest started a topic Split string like 'something.jpg,123' into $filename and $number

    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.j pg
    $matches['number'] = 123


    and ideally

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

    ?


    Martin Lucas-Smith www.geog.cam.ac.uk/~mvl22


Working...