How to seperate Model Numbers from a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahulephp
    New Member
    • Sep 2009
    • 59

    #1

    How to seperate Model Numbers from a string

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

    For Ex 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
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    If the products are consistent you could build a list of keywords to be removed. For example you have list that includes sony, panasonic, casio, camera etc. Then run your strings through a function to remove any keywords.

    Just a thought though... there maybe a more elegant solution out there.

    Comment

    • rahulephp
      New Member
      • Sep 2009
      • 59

      #3
      I got the solutions in another way,
      Here it is:
      Code:
      <?php
      $titles = Array('Sony DCSW380B 14MP Camera', 'Casio 10MP Camera EX-Z3/3', 'Panasonic Lumix Camera DMC-G1 12MP');
      
      foreach($titles as $item)
      {
          if(preg_match_all('/\b([A-Z0-9\/\-\+]+)\b/', $item, $tmp))
              print implode('||', $tmp[1]).'<br />';
      }
      
      ?>
      The output is separated by "||" in case if i need it in future.

      Comment

      Working...