Jump to content

How to seperate Model Numbers from a string


rahulephp

Recommended Posts

Can anyone please let me know how to separate Model Numbers from a string

 

ForEx Titles:

1) Sony DCSW380B 14MP Camera

2) Casio 10MP Camera EX-Z3/3

3) Panasonic Lumix Camera DMC-G1 12MP

 

Output would be:

For 1) "DCSW380B 14MP" OR ""DCSW380B"

For 2) "10MP EX-Z3/3" OR "EX-Z3/3"

For 3) "DMC-G1 12MP" OR "DMC-G1"

 

Criteria:

1) All letters of model number are CAPITAL

2) It may include alphabets, numbers or special symbol like "/", "-"

3) Model numbers can be anywhere in the title (not a specific position like 2nd in array on explode)

 

Not sure if i need to use preg_match function.

 

Please assist. Thanks in advance

Link to comment
Share on other sites

Are you storing the information? Where is the information coming from? You need either a reference or delimit character to do this easily. If it is a must there are a variety of ways to do this but it's like pushing s square peg into a round hole... it's possible if you cut up the peg and glue it back together later just not a good idea unless a must... please explain the scenario

 

Link to comment
Share on other sites

Your criteria contains information on what the model number can contain, but it does not categorically state the specific rules on how to differentiate a model number from other text. Based upon your examples, I *think* a possible rule is that any string which contains at least a single number and no lower case letters should be considered a model number. Without knowing ALL the possible inputs I can't say if this is a good rule or not. You will need to make that determination.

 

The problem with things such as this is that humans are very good at making judgements whereas computers are not. Computers simply apply the criteria they are given. So, unless you know what all the possible inputs and expected outputs are you won't know if your rules will create false positives or negatives. For example, let's say you have the following product "Simtex PLAY4SURE Recorder XPR45XZ". You and I would be able to understand that "PLAY4SURE" is the product name and not the model number - but how would a computer know that?

 

Here is a possible solution. It may not be the most efficient, but it works according to the rules I stated at the top

function getModelNumber($productName)
{
    $modelNameParts = array();
    $words = explode(' ', $productName);
    foreach($words as $word)
    {
        if(preg_match('#[0-9]+#', $word) && !preg_match('#[a-z]+#', $word))
        {
            $modelNameParts[] = $word;
        }
    }
    return implode(' ', $modelNameParts);
}

echo getModelNumber('Sony DCSW380B 14MP Camera');
//Output: DCSW380B 14MP
echo getModelNumber('Casio 10MP Camera EX-Z3/3');
//Output: 10MP EX-Z3/3
echo getModelNumber('Panasonic Lumix Camera DMC-G1 12MP');
//Output: DMC-G1 12MP

 

Link to comment
Share on other sites

The criteria says it had to be all capital letters...  but this never says that the other text can never be all capital letters... But on the criteria given I just did a strtok() then made each token upper case and compared them... was a simple fix but that's why I was proding to see what he really wanted and to be sure this wasn't h/w...

 

<?php
if($_POST['camera'] != "")
{
$cam = $_POST['camera'];
echo "You entered: ".$cam.". <br>";

$cam_token = strtok($cam, " ");
echo "Model Number is: ";
while ($cam_token != false)
  {

  $up = strtoupper($cam_token);
  
  if(strcmp($cam_token, $up) == 0){ echo "$cam_token ";}
  
  $cam_token = strtok(" ");
  }
echo ".";
}
?>

<html>
<form action="teststr.php" method="post">
<p><input type="text" name="camera"></p>
<p><input type="submit" name="submit" value="Get Model" /></p>
</form>
<body>
</body>
</html>

Link to comment
Share on other sites

OK, after a little research I have greatly improved the code. Again, this returns the word parts that contain at least one number and no lower case letters:

function getModelNumber($productName)
{
    preg_match_all('#(?=\b.*[0-9]+.*\b)\b[^a-z]*\b#', $productName, $modelNameParts);
    return implode(' ', $modelNameParts[0]);
}

 

If the model number can be a string with just upper case letters and does not require a number, then the regular expression is much easier since all it needs to do is ensure there are no lower case letters:

function getModelNumber($productName)
{
    preg_match_all('#\b[^a-z]*\b#', $productName, $modelNameParts);
    return implode(' ', $modelNameParts[0]);
}

 

EDIT: Moving this topic to the RegEx forum where it belongs.

Link to comment
Share on other sites

Nice, I like that.

One question mj... out of my own curiosity how do we determine this if it can but doesn't necessarily contain numbers and special characters

 

The second solution does exactly that. The regular expression returns all word matches that do not contain any lower case letters. So it can contain anything else: upper case letters, numbers, or special characters.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.