By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,953 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,953 IT Pros & Developers. It's quick & easy.

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

P: n/a


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 www.geog.cam.ac.uk/~mvl22
www.lucas-smith.co.uk

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <mv***@cam.ac.uk> wrote in message
news:Pi**************************************@oran ge.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 www.geog.cam.ac.uk/~mvl22
www.lucas-smith.co.uk

Jul 17 '05 #2

P: n/a
"Martin Lucas-Smith" <mv***@cam.ac.uk> schrieb im Newsbeitrag
news:Pi**************************************@oran ge.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
Jul 17 '05 #3

P: n/a
Martin Lucas-Smith <mv***@cam.ac.uk> wrote in message news:<Pi**************************************@ora nge.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


/^(\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
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.