473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

problem using array and substr

Hi,

I'm trying to write a function that takes a string and splits this into 3
distinct sections for a project I'm working on.

This string in question has the following formet:
"% 999990008312?;0008312999990000?"
The important bits i want to extract are the 1st and second numbers. In this
example that is 99999 and 0008312. The sequence of 9's has a fixed length
of 5 chars and the other number is whatever follows those 5 until the
questionmark.

My idea was to use explode to get the string up to the "?" and the use
substr() to split the remaining string into the bits I want. Not being the
greatest coder in the world I came up with this ($card_string is from the
function call):
$card_string = trim($card_string, "%"); // get rid of the first char and
all those annoying spaces
$card_data = explode("?", $card_string);
$card_ver = substr($card_data[0],0,5); // first 5 chars of string
$card_id = substr("$card_data[0]",6); // all other chars

I expected $card_ver to become "99999" and $card_id to contain "0008312",
BUT what I actually get is $card_ver empty and $card_id "999990008312". If
I insert a line $card_data[0] = "999990008312" this actually does happen,
but if I knew the string's content, why would I write a function?
Typecasting $card_data[0] to type string with either
'$card_ver_string_typed = (string) substr($card_data[0],0,5);' or
$card_data_string = "$card_data[0]" don't help either.

Am I overlooking something, is this a very weird limitation of PHP or is
there just an easier way of doing this alltogether?

All help more than welcome!

Patrick Londema
Debian GNU/Linus Testing
PHP 4.1.2
Apache 1.3.26

Hope this doesn't show up twice anywhere, newsreader gave me a really big
error message when I tried to post...
Jul 17 '05 #1
1 1838
Patrick Londema wrote:
My idea was to use explode to get the string up to the "?" and the use
substr() to split the remaining string into the bits I want. Not
being the greatest coder in the world I came up with this
($card_string is from the function call):
This is where the power of regular expressions kicks in:

$s = "% 999990008312?;0008312999990000?";

if (preg_match("/(\d{5})(\d+)/", $s, $match)) {
array_shift($match);
print_r($match);
}
PHP 4.1.2


Time to upgrade to the 4.3 branch at least...
JW

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: | last post by:
I thought I understood how to traverse an array, but I guess I was wrong. I have tried writing the code snippet below as: while, for, foreach... and get the same (consistently wrong) result each...
0
by: Moluske | last post by:
The web site was on another server i switch to a new one but this server hav now PHP Safe Mode to ON so i get error when creating thumbnail. I cant put PHP Safe mode to OFF. is ther any thing i...
9
by: Georg Ringer | last post by:
Hi, I've got a multidimensional array which looks like this: $data = array('nname' => 'Auerbach', 'vname' => 'Berthold', 'uid' => 20); $data = array('nname' => 'Bloch', 'vname' => 'Ernst', 'uid'...
10
by: rxl124 | last post by:
I have files that I only need one field that I need to grep it out and I am trying to assign that to another file. (It happens that one field that I am looking for has some other character that...
22
by: guitarromantic | last post by:
Hey everyone, I run a site with staff-submitted reviews, and most of them are written by one author. However, we also do "multiple" reviews. Up until now I just had a userid for a 'Multiple'...
2
by: Event Horizon | last post by:
Hi, I'm trying to add an simple upload applet to shopping cart script. My new applet form sends all needed post fields ( quantity, product, etc... ) but the "file" post field is hardcoded in...
1
by: Chad | last post by:
I am having a problem with substr() working when I enter the third perimeter. I am working on a price monitoring project that will download data from Amazon.com. After is downloads the page, it...
1
by: neovantage | last post by:
Hey all, I am using a PHP script which creates headings at run time in a sense at page execution. I am stuck a with a very little problem which i am sure i will have the solution from experts. ...
2
by: swethak | last post by:
Hi, I am getting the problem the problem with google map in Internet Explorer. This map worked fine in mozilla . When i opened the same map in Internet Explorer i am getting the error...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have 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...

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.