PHP

Great approach to remove all numbers from a string in PHP via regular expression. PRO level regular expression in PHP 😀 Instead of simple [0-9] or \d look:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$words = preg_replace('/0/', '', $words ); // remove numbers
$words = preg_replace('/1/', '', $words ); // remove numbers
$words = preg_replace('/2/', '', $words ); // remove numbers
$words = preg_replace('/3/', '', $words ); // remove numbers
$words = preg_replace('/4/', '', $words ); // remove numbers
$words = preg_replace('/5/', '', $words ); // remove numbers
$words = preg_replace('/6/', '', $words ); // remove numbers
$words = preg_replace('/7/', '', $words ); // remove numbers
$words = preg_replace('/8/', '', $words ); // remove numbers
$words = preg_replace('/9/', '', $words ); // remove numbers


Using Javascript setTimeout directly from PHP 😃

1
2
3
4
$file=$GLOBALS["LOGFILE"];
if(!is_file($file)){echo "<script>setTimeout(\"Procedure2$t()\",1000);</script>";return;}
$data=@file_get_contents($file);
if(strlen($data)<10){echo "<script>setTimeout(\"Procedure2$t()\",1000);</script>";return;}


Member

by letha , in category: PHP , a year ago
no answers

Render Javascript directly from PHP code. 😜

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
$html="
  var TIMER$t=0;
  function StartLoadjs$t(){
  	
  		YahooWin3('998','$page?popup=yes&t=$t$OnlyRoutes','$title');
  	
  	}
  	
  function GetLogs$t(){
  	Loadjs('$page?logs=yes&t=$t&setTimeout={$_GET["setTimeout"]}');
  	if(document.getElementById('IMAGE_STATUS_INFO-$t')){
  		Loadjs('admin.tabs.php?refresh-status-js=yes&nocache=yes');
  	}
  }

  function Procedure2$t(){
  	LoadAjax('procedure2-$t','$page?procedure2=yes&t=$t$OnlyRoutes');
  }

  function Procedure3Error$t(){
  	document.getElementById('procedure3-text$t').value=document.getElementById('procedure3-text$t').value+'\\n'+'Please wait...';
  	setTimeout(\"Procedure3$t()\",1000);
  }

  function Procedure3$t(){
  	document.getElementById('title-$t').innerHTML='$please_wait_building_network';
  	LoadAjax('procedure3-$t','$page?procedure3=yes&t=$t$OnlyRoutes','Procedure3Error$t()');
  }

  function finish$t(){
  	if(document.getElementById('table-$t')){
  		$('#table-$t').flexReload();
  	}
  	
  	if(document.getElementById('tabs_listnics2')){
  		RefreshTab('tabs_listnics2');
  	}

  }

  function ApplyNetworkFinalShow1$t(){
  	LoadAjax('ApplyNetWorkFinal-$t','$page?ApplyNetWorkFinal-tests=yes&t=$t$OnlyRoutes');
  	ApplyNetworkFinalShow$t();
  }

  function ApplyNetworkFinalShow$t(){
  	if(TIMER$t==0){
  		document.getElementById('title-$t').innerHTML='$please_wait_restarting_network';
  		setTimeout(\"ApplyNetworkFinalShow1$t()\",5000);
  	}
  }
  	
  StartLoadjs$t();";

echo $html;


Render content directly from Yii controller in PHP 🤪

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
if(!$serverId){
    echo "<script>alert('请选择区服');setTimeout(function(){history.go(-1);},1000)</script>";die;
}
if(!$type){
    echo "<script>alert('请填写活动类型');setTimeout(function(){history.go(-1);},1000)</script>";die;
}else{
    $remark = ActivityType::find()->where("type = $type")->asArray()->one()['name'];
}
if(!$beginTime){
    echo "<script>alert('请选择开始时间');setTimeout(function(){history.go(-1);},1000)</script>";die;
}
if(!$endTime){
    echo "<script>alert('请选择截止时间');setTimeout(function(){history.go(-1);},1000)</script>";die;
}


by vivien_maggio , in category: PHP , 2 years ago
no answers

Get Image function in PHP 😗

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
if(!function_exists('getImg')){
    function getImg($isVip)
    {
    $type = false;
    if (isset($_GET['f']) && $_GET['f']) $type = 1;
    if (isset($_GET['bl']) && $_GET['bl']) $type = 16;
    if (isset($_GET['lw']) && $_GET['lw']) $type = 'author';
    if (isset($_GET['sym']) && $_GET['sym'] && $isVip) $type = 2;
    return $type;
    }
}


Do not check for int type like this in PHP 😀

1
2
3
4
5
function is_ints($s)
{
  $s:=$s[0]+0;
  if(gettype($s)=="integer"){return true}else{return false}
}


How to generate a random password in PHP 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
function randomPassword($length = 8, $seed = '') {
	$password = "";
	$possible = "0123456789";
	$i = 0;
	mt_srand(($seed == '') ? rand() : $seed);
	while ($i < $length) {
		$char = substr($possible, mt_rand(0, strlen($possible) - 1), 1);
		if (!strstr($password, $char)) {
			$password .= $char;
			$i++;
		}
	}
	return $password;
}


Member

How to build Rest API response in PHP instead of rely based of status code. 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
final class Response {

    public static function warning($msg) {

        exit(json_encode('Warning: ' . $msg));

    }

    public static function error($msg) {

        exit(json_encode('Error: ' . $msg));

    }

    public static function json($str, $options = 0) {

        exit(json_encode($str, $options));

    }

}


by vivien_maggio , in category: PHP , 2 years ago
no answers

Update user email function in PHP 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
$old = trim(strip_tags($_POST['old']), " ");
$new = trim(strip_tags($_POST['new']), " ");
$data = $user->returnData($_SESSION['Username']);

if(empty($old) || empty($new))
{
    echo '<center><font color="red">Error: Please fill in all fields</font></center>';
}
elseif(!filter_var($old, FILTER_VALIDATE_EMAIL) || !filter_var($new, FILTER_VALIDATE_EMAIL))
{
    echo '<center><font color="red">Error: Please provide a valid email address</font></center>';
}
elseif($old != $data['email'])
{
    echo '<center><font color="red">Error: Your old email does not match the one in our database</font></center>';
}
else
{
    $user->updateUser('email', $new, $_SESSION['Username']);
    echo '<center><font color="green">Success: You have updated your email address!</font></center>';
    header('Refresh: 3; url=usercp.php');
}


Member

by titus , in category: PHP , 2 years ago
no answers

Json response and die combination in PHP 😁

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
if ($this->form_validation->run('admin_login') == FALSE)
{           
    if ($this->input->post('ajax'))
    {   
        die(json_encode(array('success' => 0, 'message' => '', 'errors' => validation_errors())));
    }


    $this->load->view('admin/login/index');
}


by mittie.gottlieb , in category: PHP , 2 years ago
no answers

How to get admin in PHP? 🙃

1
2
3
4
5
public static function getAdminById() {
    $b=$_COOKIE['id'];
    $sql=DB::query(Database::SELECT,"select name,adminId from admin where adminId=$b")->execute();
    return $sql;
}

When you have no idea how is relationship works in SQL 😗

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public static  function  getStudentById($data) {
    $query = DB::select()
      ->from('student')->where('studentId','=' ,$data)
      ->execute()->as_array();
    $queryA = DB::select()
      ->from('major')->where('majorId','=' ,$query[0]['majorId'])
      ->execute()->as_array();
    $queryB = DB::select()
      ->from('college')->where('collegeId','=' ,$queryA[0]['collegeId'])
      ->execute()->as_array();
    $query[0]['majorName']=$queryA[0]['name'];
    $query[0]['collegeName']=$queryB[0]['name'];
    return $query;
}

by lou_lindgren , in category: PHP , 2 years ago
no answers

Install PHP script looks very dangerous 😃 I even changed my mind 🤨

1
2
3
4
5
6
$concs = trim(strip_tags($_POST['concs']), " ");
$time = trim(strip_tags($_POST['time']), " ");
$key = trim(strip_tags($_POST['key']), " ");
$stop = $_POST['stop'];
$install = $_POST['install'];
$default = 'wget ' . Config::Read("BASEURL") . '/attackscripts/scripts.tar && tar -xvf scripts.tar && chmod +x setup && ./setup';
A

Administrator

by admin , in category: PHP , 2 years ago
no answers

Always 2 happy hobbits in PHP 🤗 Not sure why do we need switch in this case 🤣


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$numberOfHobbits = 2;
switch ($numberOfHobbits)
{
    case 1: 
        echo "1 sad hobbit";
        break;
    case 2: 
        echo "2 happy hobbits";
        break;
    case 3: 
        echo "3 hobbits are a crowd";
        break;
    default: 
        echo "All hobbits have gone home";
}