0

Hi Friends,
There is a variable call 'ReportID'
Its values are normally in this format. 'MPS141', 'MPS142'....
'MP' means company name. 'S' means, Songs and '14' means year 2014
and other part is auto incrementing data.
I want to get onlye that value.
Is there any function in PHP ?
help me guys

4
Contributors
4
Replies
30
Views
3 Years
Discussion Span
Last Post by jKidz
3

Hi, you can use substr():

<?php

$reportID = 'MPS141';

echo substr($reportID, 5);

Will return all the characters after MPS14, but you can also create a function to return an array of all the elements of the reportID, an example:

function reportToArray($id)
{
    $data['CompanyName']    = substr($id, 0, 2);
    $data['Article']        = substr($id, 2, 1);
    $data['Year']           = substr($id, 3, 2);
    $data['ID']             = substr($id, 5);
    return $data;
}

print_r(reportToArray($reportID));

Returns:

Array
(
    [CompanyName] => MP
    [Article] => S
    [Year] => 14
    [ID] => 1
)

Docs: http://www.php.net/manual/en/function.substr.php

Votes + Comments
thanks buddy
0

Hi

I belive cereal has clearly show the way it can be done. USe the substring fucntion and store the data in a json is ideal way of doing it.

2

I think cereal's substr is probably the easiest. However, you do not indicate whether the data is in a constant-length format (not including the increment part).

This will obviously work if you have the format:

  • Company Name - always 2 chars
  • Category - always 1 char
  • Year - always 2 digits
  • Increment - can vary (not important)

If however the company name can vary, but the category is always a single char or the category can vary but the company name is always 1 char, it can still be done with a regex pattern.

In addition - list can often be handy to use when dealing with pieces of data, e.g. using cereal's function...

function reportToArray($id)
{
    $data['CompanyName']    = substr($id, 0, 2);
    $data['Article']        = substr($id, 2, 1);
    $data['Year']           = substr($id, 3, 2);
    $data['ID']             = substr($id, 5);
    return $data;
}

list($company,$article,$year,$id) = reportToArray($id);
Votes + Comments
thanks buddy
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.