rahulephp Posted June 23, 2010 Share Posted June 23, 2010 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 Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted June 23, 2010 Share Posted June 23, 2010 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 Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted June 23, 2010 Share Posted June 23, 2010 Ok out of curiosity I wrote a script that seems to have done this... A lot simpler and easier then I thought. Let me know what you having issues with & I can now point you in right direction Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 23, 2010 Share Posted June 23, 2010 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 Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted June 23, 2010 Share Posted June 23, 2010 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> Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 23, 2010 Share Posted June 23, 2010 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. Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted June 23, 2010 Share Posted June 23, 2010 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 23, 2010 Share Posted June 23, 2010 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. Quote Link to comment Share on other sites More sharing options...
gwolgamott Posted June 23, 2010 Share Posted June 23, 2010 Doh! Yeah it does. Brain fart sorry.... Thanks. I'll have to note your solution in case I'll ever need something like this. BTW whatever happened to the person who put this here to begin with? Quote Link to comment Share on other sites More sharing options...
rahulephp Posted June 24, 2010 Author Share Posted June 24, 2010 Excellent mjdamato and gwolgamott , It works for me. Thanks in bunch Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.