Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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