PHP Getting certain parts of a string
I have the following tire sizes in my database, all formatted in different ways. Here are a few examples:
225/50R17
255/45R18/XL
P155/80R13
What I need is to separate them in 3 parts, and JUST the numbers.
So the first one should be: 225 / 50 / 17 all separate variables. Second one should be: 255 / 45 / 18 and ignore the XL. And the third one obviously should be 155 / 80 / 13
Does anyone know how I can write a function or whatever needs to be done to开发者_运维技巧 just grab those numbers?
Thank you!
You probably want something like this:
$ar = array( '225/50R17', '255/45R18/XL', 'P155/80R13' ); foreach ($ar as $a) { if (preg_match_all('/(\d+)/', $a, $match)) { print "match=[". print_r($match[0], true) . "]\n"; } } ?>
Which produces output like this:
match=[Array ( [0] => 225 [1] => 50 [2] => 17 ) ] match=[Array ( [0] => 255 [1] => 45 [2] => 18 ) ] match=[Array ( [0] => 155 [1] => 80 [2] => 13 ) ]
You could use the preg_split()
function:
$str = '255/45R18/XL';
$chars = preg_split('/[^\d]/', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);
This results in:
Array
(
[0] => 255
[1] => 45
[2] => 18
)
You'll probably want to have the PREG_SPLIT_NO_EMPTY
flag in there, otherwise you'll end up with empty elements in your array.
You're essentially splitting on characters that aren't digits, so preg_split()
seems like the natural choice.
Assuming the numbers will always have at least 2 digits a regular expression like this will work:
(\d{2,})\/(\d{2,}).*?(\d{2,})
Demo: http://rubular.com/r/l3IDSzU7oH
http://sandbox.phpcode.eu/g/3cb0f
<?php
$string = '225/50R17';
$filtered = preg_replace('~[a-z]+~i', '/', $string);
$exploded = explode('/', $filtered);
foreach($exploded as $one){
if (!empty($one)){
echo "<b> ".$one."</b>";
}
}
Providing it's always an R that splits numbers, this code should work (untested):
$string = // Get the current row from the database
// Replace all occurences of `R` with a `/`
str_replace("R", "/", $string);
// Remove any letters
$string = preg_replace("/[a-zA-Z]/i", "");
// Split string into an array
$numbers = explode("/", $string);
// Get rid of any empty elements
foreach($numbers as $index => $value)
{
if(empty($value))
{
unset($numbers[$value]);
}
}
There are probably better ways to do it, but this should work for most if not all cases.
Do bear in mind that this is only for one row and should be put in your MySQL while()
loop or whatever. You can then append $numbers
to an array holding all tyre sizes or whatever you'd like to do.
one way to do it without regexp just in case your strings aren't that predictable
function remove_crap ($crapString) {
//build an alphabet array
for ($character = 65; $character < 91; $character++) {
$potentialCrap[] = chr($character);
}
//throw in slash or whatever other crap there might be with the numbers
$potentialCrap[] = "/";
//strip out all potential crap
$newString = str_replace($potentialCrap, '', $crapString);
//readd the slashes where they go, totally better ways to do this but i'm tired and want to go home
$finalString = $newString[0].$newString[1].$newString[2]."/".$newString[3].$newString[4]."/".$newString[5].$newString[6];
return $finalString;
}
You can use a regular expression to do this:
$text = "255/45R18/XL";
$matches = array();
preg_match_all("/(\d+)/", $text, $matches);
var_dump($matches);
After preg_match_all $matches will look like:
array(4) {
[0]=>
string(12) "255/45R18/XL"
[1]=>
string(3) "255"
[2]=>
string(2) "45"
[3]=>
string(2) "18"
}
This works for me
<?php
$s = "145/80R13";
$x = preg_split('/\/|[a-z]|[A-Z]|-| /', $s);
print_r($x);
?>
精彩评论