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 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
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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...
|
by: jack |
last post by:
Hello,
I need to plit: "1,34,54,123,34"
Into an array list.
Thanks for any help,
Jack
|
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...
| |
by: L. Scott M. |
last post by:
Have a quick simple question:
dim x as string
x = "1234567890"
-------------------------------------------------------
VB 6
dim y as string
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |