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
How to seperate Model Numbers from a string
Moderator: General Moderators
-
ronnyzmanbarzell
- Forum Newbie
- Posts: 3
- Joined: Wed Jun 23, 2010 10:48 am
Re: How to seperate Model Numbers from a string
Here's a PHP script that will do the following. I did this in a rush, so there may be better ways of doing this, but it works for your example.
IIRC, your criteria were not strict enough to uniquely identify model numbers as I had spurious matches, so I tweaked the regular expressions. The heart is the definition of $patterns. I'm using two pattern matches. The first matches a model in the string, the second a model at the end of the string. In this case, a model is any contiguous series of capital letters, numbers, dashes, and/or slashes followed by a string or occurring at the end of the line.
You are right that preg_match is the way to go.
[text]<html>
<body>
<?
$strings = array(
'1) Sony DCSW380B 14MP Camera',
'2) Casio 10MP Camera EX-Z3/3',
'3) Panasonic Lumix Camera DMC-G1 12MP'
);
$patterns = array(
'/([A-Z0-9\-\/]+\s)/',
'/([A-Z0-9\-\/]+)$/'
);
foreach ($strings as $s) {
print "<p><u>$s</u><br>";
foreach ($patterns as $p) {
preg_match($p, $s, $matches);
for ($i=1; $i<count($matches); $i++) {
print "$matches[$i]<br>";
}
}
}
?>
</body>
</html>[/text]
IIRC, your criteria were not strict enough to uniquely identify model numbers as I had spurious matches, so I tweaked the regular expressions. The heart is the definition of $patterns. I'm using two pattern matches. The first matches a model in the string, the second a model at the end of the string. In this case, a model is any contiguous series of capital letters, numbers, dashes, and/or slashes followed by a string or occurring at the end of the line.
You are right that preg_match is the way to go.
[text]<html>
<body>
<?
$strings = array(
'1) Sony DCSW380B 14MP Camera',
'2) Casio 10MP Camera EX-Z3/3',
'3) Panasonic Lumix Camera DMC-G1 12MP'
);
$patterns = array(
'/([A-Z0-9\-\/]+\s)/',
'/([A-Z0-9\-\/]+)$/'
);
foreach ($strings as $s) {
print "<p><u>$s</u><br>";
foreach ($patterns as $p) {
preg_match($p, $s, $matches);
for ($i=1; $i<count($matches); $i++) {
print "$matches[$i]<br>";
}
}
}
?>
</body>
</html>[/text]
Re: How to seperate Model Numbers from a string
Excellent Ronnyzmanbarzell, It works for me.
Thanks in bunch
Thanks in bunch