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

Recommended Answers

All 4 Replies

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

commented: 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.

Member Avatar for diafol

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);
commented: thanks buddy +0

Thanks All
Thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.