Jump to content

[SOLVED] preg_split


didgydont

Recommended Posts

make this smaller

trying to convert this 530pm to $1 = 5 $2= 30 $3=pm

this works for now but i want to make it tiny

function b4ampm($b4apptime){
if(ereg("^[a-z0-9]{5}$", $b4apptime)){
$b4a = preg_split('//', $b4apptime, -1, PREG_SPLIT_NO_EMPTY);
return $b4a[3] . $b4a[4] ;}
if(ereg("^[a-z0-9]{6}$", $b4apptime)) {
$b4a = preg_split('//', $b4apptime, -1, PREG_SPLIT_NO_EMPTY);
return $b4a[4] . $b4a[5] ;}
}

function b4mins($b4apptime){
if(ereg("^[a-z0-9]{5}$", $b4apptime)){
$b4a = preg_split('//', $b4apptime, -1, PREG_SPLIT_NO_EMPTY);
return $b4a[1] . $b4a[2] ;}
if(ereg("^[a-z0-9]{6}$", $b4apptime)) {
$b4a = preg_split('//', $b4apptime, -1, PREG_SPLIT_NO_EMPTY);
return $b4a[2] . $b4a[3] ;}
}

function b4hour($b4apptime){
if(ereg("^[a-z0-9]{5}$", $b4apptime)){
$b4a = preg_split('//', $b4apptime, -1, PREG_SPLIT_NO_EMPTY);
return $b4a[0] ;
}
if(ereg("^[a-z0-9]{6}$", $b4apptime)) {
$b4a = preg_split('//', $b4apptime, -1, PREG_SPLIT_NO_EMPTY);
return $b4a[0] . $b4a[1] ;
}
}

Link to comment
https://forums.phpfreaks.com/topic/164649-solved-preg_split/#findComment-868337
Share on other sites

What are the possible formats it could be? 24hour possible? leading zeros possible? etc. You certainly don't need all that...

 

Perhaps something like:

 

$str = '530pm';

preg_match('/(0?[0-9]|1[0-2])(0?[0-9]|[1-5][0-9])(am|pm)/i', $str, $matches);

print_r($matches);

 

Can make it more accurate once we know all the possible formats though...

Link to comment
https://forums.phpfreaks.com/topic/164649-solved-preg_split/#findComment-868353
Share on other sites

thanx guys all possible formats are like so

"900am", "915am", "930am", "945am", "1000am", "1015am", "1030am", "1045am", "1100am", "1115am", "1130am", "1145am", "1200pm", "1215pm" and so on

thats why i had if(ereg("^[a-z0-9]{5}$", $b4apptime)) and if(ereg("^[a-z0-9]{6}$", $b4apptime))

 

i used your help to do this an example

 

$str = "1245pm";

if(ereg("^[a-z0-9]{5}$", $str)){

preg_match('/([0-9]{1})([0-9]{2})(am|pm)/', $str, $matches);}

if(ereg("^[a-z0-9]{6}$", $str)){

preg_match('/([0-9]{2})([0-9]{2})(am|pm)/', $str, $matches);}

print_r($matches);

answer = Array ( [0] => 1245pm [1] => 12 [2] => 45 [3] => pm )

 

wich will shrink the codes lots but i dont under stand why the array put in the original value ?

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/164649-solved-preg_split/#findComment-868993
Share on other sites

Realise this is closed now but, you could improve that further:

 

if (strlen($str) == 5)
{
    $pattern = '/([0-9]|1[0-2])(0[0-9]|[1-5][0-9])(am|pm)/';
}
elseif (strlen($str) == 6)
{
    $pattern = '/(1[0-2])(0[0-9]|[1-5][0-9])(am|pm)/';
}

preg_match($pattern, $str, $matches);

Link to comment
https://forums.phpfreaks.com/topic/164649-solved-preg_split/#findComment-869149
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.