如何简化此代码以避免为3个不同的值重现相同的代码!?例如,在当前代码中,我解析3个URL地址以确保它们被定向到正确的目的地,因此人们不会设置各种疯狂的URL地址。但是在将来,我可能会扩展到更多的社交媒体网址,一遍又一遍地重复代码将会很繁琐。谢谢你的时间!
if(isset($_POST['submit'])){
$facebook = strtolower($_POST['facebook']);
$twitter = strtolower($_POST['twitter']);
$youtube = strtolower($_POST['youtube']);
$fb_exp = explode("facebook.com", $facebook);
$tw_exp = explode("twitter.com", $twitter);
$yt_exp = explode("youtube.com", $youtube);
if(($fb_exp[0] == 'http://' || $fb_exp[0] == 'http://www.' || $fb_exp[0] == 'https://' || $fb_exp[0] == 'https://www.' || $fb_exp[0] == '') && $facebook != ''){
$facebook = 'https://www.facebook.com'.$fb_exp[1];
$fb = 1;
}elseif($facebook == ''){
$facebook = '';
$fb = 1;
}else{
echo 'Not a Valid Facebook Page !';
}
if(($tw_exp[0] == 'http://' || $tw_exp[0] == 'http://www.' || $tw_exp[0] == 'https://' || $tw_exp[0] == 'https://www.' || $tw_exp[0] == '') && $twitter != ''){
$twitter = 'https://www.twitter.com'.$tw_exp[1];
$tw = 1;
}elseif($twitter == ''){
$twitter = '';
$tw = 1;
}else{
echo 'Not a Valid Twutter Page !';
}
if(($yt_exp[0] == 'http://' || $yt_exp[0] == 'http://www.' || $yt_exp[0] == 'https://' || $yt_exp[0] == 'https://www.' || $yt_exp[0] == '') && $youtube != ''){
$youtube = 'https://www.youtube.com'.$yt_exp[1];
$yt = 1;
}elseif($youtube == ''){
$youtube = '';
$yt = 1;
}else{
echo 'Not a Valid YouTube Page !';
}
if($fb == 1 && $tw == 1 && $yt == 1){
$text= '<?php $title="'.$_POST['title'].'";
$subtitle="'.$_POST['subtitle'].'";
$email="'.$_POST['email'].'";
$tel="'.$_POST['tel'].'";
$add="'.$_POST['add'].'";
$facebook="'.$facebook.'";
$twitter="'.$twitter.'";
$youtube="'.$youtube.'";
';
$fp = fopen('text.ini','w');
fwrite($fp,$text);
fclose($fp);
header('Location: http://somesite.com/index.php');
}
}
答案 0 :(得分:1)
使用循环,例如foreach
:
if(isset($_POST['submit'])){
$valid = true;
$links = [];
$list = ['facebook' => 'facebook.com', 'twitter' => 'twitter.com', 'youtube' => 'youtube.com'];
foreach($list as $key => $domain) {
if(isset($_POST[$key])) {
$exp = explode($domain, strtolower($link));
if($link != '' && ($exp[0] == 'http://' || $exp[0] == 'http://www.' || $exp[0] == 'https://' || $exp[0] == 'https://www.' || $exp[0] == '')){
$links[$key] = 'https://www.'.$domain.$exp[1];
}elseif($link == ''){
$links[$key] = '';
}else{
echo 'Not a Valid '.ucfirst($key).' Page !';
$valid = false;
}
}
}
if($valid) {
$text = '<?php $title="'.$_POST['title'].'";
$subtitle="'.$_POST['subtitle'].'";
$email="'.$_POST['email'].'";
$tel="'.$_POST['tel'].'";
$add="'.$_POST['add'].'";
$facebook="'.$links['facebook'].'";
$twitter="'.$links['twitter'].'";
$youtube="'.$links['youtube'].'";
';
$fp = fopen('text.ini','w');
fwrite($fp,$text);
fclose($fp);
header('Location: http://somesite.com/index.php');
}
}
正则表达式而不是许多条件:
foreach($list as $key => $domain) {
if(isset($_POST[$key]) && preg_match('#^(?:https?://(?:www\.)?)?('.$domain.'.*)$#i', strtolower($_POST[$key]), $matches)) {
$links[$key] = 'https://www.' . $matches[1];
} else {
echo 'Not a Valid '.ucfirst($key).' Page !';
$valid = false;
}
}
答案 1 :(得分:1)
您可以使用parse_url
进行简化并使用循环来处理数组:
$smPass = 0;
$correctUrls = array('facebook'=>'https://www.facebook.com','twitter'=>'https://www.twitter.com','youtube'=>'https://www.youtube.com');
$output = array('facebook'=>'','twitter'=>'','youtube'=>'');
foreach($_POST as $social=>$value){
if(in_array( $social, array_keys($correctUrls) )) {
if(empty($value)){
$smPass += 1;
}
else {
if($urlParts = parse_url($value)) {
if( in_array($urlParts['host'], array_keys($correctUrls)) ) {
$output[$social] = $correctUrls[$social].$urlParts['path'].'?'.$urlParts['query'];
$smPass += 1;
}
else {
echo 'Invalid '.$social;
}
}
else {
echo 'Invalid '.$social;
}
}
}
}
if($smPass == 3){
$text= '<?php $title="'.$_POST['title'].'";
$subtitle="'.$_POST['subtitle'].'";
$email="'.$_POST['email'].'";
$tel="'.$_POST['tel'].'";
$add="'.$_POST['add'].'";
$facebook="'.$output['facebook'].'";
$twitter="'.$output['twitter'].'";
$youtube="'.$output['youtube'].'";
';
$fp = fopen('text.ini','w');
fwrite($fp,$text);
fclose($fp);
header('Location: http://somesite.com/index.php');
}