###前提・実現したいこと
いつもお世話になっています。
フリー入力欄に入れている金額を取り出して、半角数字の整数にしたいのですが、もう少し簡略化したく思います。
入力時にバリデーションしてほしいところですが、諸事情でできません。
入力する人もちゃんとチェックしてないようで全角が混ざっていたり、3桁区切りのカンマがドットだったり、まったく関係ない文字が入っていたりします。。。
以下のように整形して整数にしているのですが、もう少しすっきりさせることができるのではないかと思いましてお知恵を拝借したく思います。
特にif(preg_match("/^[0-9]+$/",$rent_amount[$j]))の部分はまったく同じなので整理したいです。
###該当のソースコード
$j=0; $replace = array('.',','); //賃料の取得 //賃料の取得 $rentamount = str_replace("万円", "",$url2->rent_amount);//20万円などの形式で入力されている想定 $rentamount = mb_convert_kana($rentamount, "a"); $rent_amount[$j] = sprintf('%.0f', $rentamount * 10000); if(preg_match("/^[0-9]+$/",$rent_amount[$j])){ } else { $rent_amount[$j] = ""; } if(!empty($url2->room_keyword1)){ $j++; $room_keyword1 = str_replace("円", "",$url2->room_keyword1);//100,000円などの形式で入力されている想定 $room_keyword1 = mb_convert_kana($room_keyword1, "a"); $rent_amount[$j] = str_replace($replace,'',($room_keyword1)); if(preg_match("/^[0-9]+$/",$rent_amount[$j])){ } else { $rent_amount[$j] = ""; } } if(!empty($url2->room_keyword2)){ $j++; $room_keyword2 = str_replace("円", "",$url2->room_keyword2);//100,000円などの形式で入力されている想定 $room_keyword2 = mb_convert_kana($room_keyword2, "a"); $rent_amount[$j] = str_replace($replace,'',($room_keyword2)); if(preg_match("/^[0-9]+$/",$rent_amount[$j])){ } else { $rent_amount[$j] = ""; } }
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/01 06:37
2016/08/01 07:01