PHP字串長度找子字串與取代字串的常用函數

PHP在字串的操作上,有非常多的相關函數可以使用,我們在這裡只介紹經常會使用到的字串操作函數。像是如何取得字串的長度、在某一個字裏面尋找子字串首次出現的位置、以及字串的取代函數...等等。這些都是在編寫程式的時候,頻繁被程式設計師使用到的基礎函數。 取得字串長度 <?...

2017年12月25日 星期一

PHP使用$_SERVER取得主機IP與網址

<?php
// 取得server ip
echo $_SERVER['SERVER_ADDR'];

// 取得目前請求的PHP頁面的完整URI
echo $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

// 取得目前請求的PHP頁面的完整實體路徑
echo $_SERVER['SCRIPT_FILENAME'];

// 取得網站的根目錄
echo $_SERVER['DOCUMENT_ROOT'];

// 取得目前請求的PHP頁面的相對URI
echo $_SERVER['REQUEST_URI'];
echo $_SERVER['SCRIPT_NAME'];
echo $_SERVER['PHP_SELF'];
?>

PHP取得隨機時間的函數

<?php
// 取得隨機時間的函數
function get_rand_time($st_time, $ed_time)
{  
  $nums = ($ed_time - $st_time)/(24*60*60);
  //echo  '1 ~ ' . $nums . '<br/>';

  $rd_num = rand(1,$nums);
  //$rand_time = $st_time + ($rd_num - 1) * 24*60*60;
  $rand_time = $st_time + ($rd_num - 1) * 24*60*60 + (rand(1,86399));
  if($rand_time > time()) $rand_time = time();
  //echo $rand_time . ' ('.$rd_num.') <br/>';
  //echo date("Y-m-d H:i:s", $rand_time);
  
  return date("Y-m-d H:i:s", $rand_time);
}

// 執行函式呼叫
$ed_time = strtotime(date("Y-m-d"));
$st_time = $ed_time - 30*24*60*60;

get_rand_time($st_time, $ed_time);

?>

PHP日期DATE指令的操作

<?php
// 取得現在時間時間戳記(Unix timestamp)
echo time();
echo mktime();

// 格式化時間
echo date('Y-m-d H:i:s', '1461827400');
echo date("Y-m-d H:i:s");

// 字串轉時間
echo strtotime("10:30pm April 15 2014");

// 設定timestamp
$timestamp = strtotime('Mon, 12 Dec 2011 21:17:52 +0000');
$dt = new DateTime();
$dt->setTimestamp($timestamp);

// 設定預設的時區
date_default_timezone_set();

// 設定timezone和format
$dtm = new DateTime(strtotime($time));
$dtm->setTimezone(new DateTimeZone(ADMIN_TIMEZONE));
$date = $dtm->format('D, M dS');
$time = $dtm->format('g:i a');

?>

PHP與SimpleXml使用CRUD

<?php
// 建立SimpleXmlElement
$config = new SimpleXmlElement('');
$config->setting1 = 'setting1 value';         
$config->saveXML('config.xml');               

// 讀取SimpleXmlElement
$config = new SimpleXmlElement('config.xml');
echo $config->setting1;
echo $config->asXml();

// 更新SimpleXmlElement
$config->setting1 = 'new value';
$config->setting2 = 'setting2 value';
echo $config->asXml();

// 刪除SimpleXmlElement
unset($config->setting1);
$config->setting2 = NULL;
echo $config->asXML();
unlink('config.xml');

?>

PHP常用字串指令的使用

<?php
$str = "電腦專家,是PRO訓練,有素的人才!";

// 字串轉小寫
echo strtolower($str);

// 字串轉大寫
echo strtoupper($str);

// 從左邊開始找PRO字串片段
echo strpos($str,'PRO');

// 從右邊開始找PRO字串片段
echo strrpos($str,'PRO');

// 取長度為5的子字串
echo substr($str, 5);

// 字串取代
echo str_replace("PRO", "專業", $str);

// 字串分割為陣列
$ss = explode(',',$str);

?>

2017年12月24日 星期日

使用GridBagLayout將每個元件放入每一格子裡面

package test.swing.layout;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class TestGridLayout extends JFrame {
 
 private JPanel contentPane;

 public static void main(String[] args) {
  TestGridLayout frame = new TestGridLayout();
  frame.setVisible(true);
 }
 
 public TestGridLayout() {
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(100, 100, 450, 300);   //(左邊界,上邊界,宽度,長度)
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));   //(上,左,下,右)
  setContentPane(contentPane);
  
  //將Layout設定為GridLayout會把視窗畫面切割成長、宽相同大小的方格
  contentPane.setLayout(new GridLayout(2, 0, 10, 5));  //(列數,欄數,左右間距,上下間距)
  
  JButton firstBtn = new JButton("按鈕 1");
  contentPane.add(firstBtn);
  
  JButton secondBtn = new JButton("按鈕 2");
  contentPane.add(secondBtn);
  
  JButton thirthBtn = new JButton("按鈕 3");
  contentPane.add(thirthBtn); 
  
  JButton forthBtn = new JButton("按鈕 4");
  contentPane.add(forthBtn); 
 }

}

執行結果

運用FlowLayout來依序排列元件,超出列宽往下一列放置元件


package test.swing.layout;

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class TestFlowLayout extends JFrame {
	
	private JPanel contentPane;

	public static void main(String[] args) {
		TestFlowLayout frame = new TestFlowLayout();
		frame.setVisible(true);

	}
	
	public TestFlowLayout() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);   //(左邊界,上邊界,宽度,長度)
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));   //(上,左,下,右)
		setContentPane(contentPane);
		
		//將Layout設定為由左至右的排列components之間的關係,超出面板宽度的元件往下列遞補
		contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));   //(置左,左右間距,上下間距)
		
		JButton firstBtn = new JButton("按鈕 1");
		contentPane.add(firstBtn);
		
		JButton secondBtn = new JButton("按鈕 2");
		contentPane.add(secondBtn);
		
		JButton thirdBtn = new JButton("按鈕 3");
		contentPane.add(thirdBtn);
		
		JButton forthBtn = new JButton("按鈕 4");
		contentPane.add(forthBtn);
		
		JButton fifthBtn = new JButton("按鈕 5");
		contentPane.add(fifthBtn);
		
		JButton sixthBtn = new JButton("按鈕 6");
		contentPane.add(sixthBtn);		
	}

}

執行結果