473,473 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

Jul 17 '05 #1
3 5978
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
3
by: alexk | last post by:
I've a simple question. Why the following: words = "123#@$#$@^% wordB#@$".split('~`!@#$%^&*()_+-={},./') doesn't work? The length of the result vector is 1. I'm using ActivePython 2.4 Alex
9
by: martin | last post by:
Hi, a very newbie question. How do I split the adress and number to 2 variables? ex. "Kingsroad 347" = variabel1 = "Kingsroad" variabel2 = "347" Ill guess i have to search the string from...
2
by: jack | last post by:
Hello, I need to plit: "1,34,54,123,34" Into an array list. Thanks for any help, Jack
5
by: lgbjr | last post by:
Hello All, I have the following type of string: "X:Y\Z.exe" "123" What I need is an array of strings with the information from within each set of quotes. I was trying to use a Regex.Split, but...
7
by: L. Scott M. | last post by:
Have a quick simple question: dim x as string x = "1234567890" ------------------------------------------------------- VB 6 dim y as string
12
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way...
14
by: nishit.gupta | last post by:
Is their any single fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" , It can also...
3
by: Patrick | last post by:
Hello All, Been beating my head on the wall trying to get this working. Especially because of my *tweener level of php and regex experience. I have this $filename = C17AA001.000 I want to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.