728x90
반응형
PHP file_get_contents()
  • 전체파일을 문자열로 읽어들이는 PHP 함수
  • 로컬파일, 원격파일 모두 가능
$str = file_get_contents('test.txt');
echo $str;
# John Smith
$str = file_get_contents('http://zetawiki.com/ex/txt/utf8hello.txt');
echo $str;
# Hi.
# 안녕.
# おはよう。


728x90
반응형

'Web Programming > php' 카테고리의 다른 글

php $_GET $_POST  (0) 2018.09.05
php new line to br nl2br()  (0) 2018.09.05
PHP 문자열 길이 (strlen, mb_strlen 함수)  (0) 2018.09.04
php data types  (0) 2018.09.04
php include, require  (0) 2018.09.03
728x90
반응형
http://www.example.com/index.html?name=john&email=john@gmail.com&contact=9877989898

Client Side: Below code is an HTML form with method=”get” for user to fill information.


<form action="#" method="get">
<input type="text" name="name" placeholder="Your Name"></input><br/>
<input type="text" name="email" placeholder="Your Email"></input><br/>
<input type="text" name="contact" placeholder="Your Mobile"></input><br/>
<input type="submit" name="submit" value="Submit"></input>
</form>

Server Side: Below code has PHP script where, $_GET associative array is used to receive sent information at server end.


<?php
if( $_GET["name"] || $_GET["email"] || $_GET["contact"])
{
echo "Welcome: ". $_GET['name']. "<br />";
echo "Your Email is: ". $_GET["email"]. "<br />";
echo "Your Mobile No. is: ". $_GET["contact"];
}
?>


Client Side: Below code is an  HTML form with method=”post” for user to fill information.


<form action="#" method="post">
....
</form>

Server Side: Below code has PHP script where, $_POST associative array  is used to receive sent information at server end.


<?php
if( $_POST["name"] || $_POST["email"] || $_POST["contact"])
{
echo "Welcome: ". $_POST['name']. "<br />";
echo "Your Email is: ". $_POST["email"]. "<br />";
echo "Your Mobile No. is: ". $_POST["contact"];
}
?>

Query string , generated by Post  method never appears in address bar i.e. it is hidden for the user therefore, we can use this method for sending sensitive information to server. Moreover, we can make use of this method to send binary data to the server without any restrictions to data size.

 

In our example, we allow user to choose a method via radio button and this value is assigned to form’s method attribute.


$("input[type=radio]").change(function(){
var method = $(this).val();
$("#form").attr("method", method);   // Assigns Method Type From Radio Button
});


728x90
반응형

'Web Programming > php' 카테고리의 다른 글

php file_get_contents()  (0) 2018.09.05
php new line to br nl2br()  (0) 2018.09.05
PHP 문자열 길이 (strlen, mb_strlen 함수)  (0) 2018.09.04
php data types  (0) 2018.09.04
php include, require  (0) 2018.09.03
728x90
반응형

Example

Insert line breaks where newlines (\n) occur in the string:

<?php
echo nl2br("One line.\nAnother line.");
?>

The browser output of the code above will be:

One line.
Another line.

The HTML output of the code above will be (View Source):

One line.<br />
Another line.


728x90
반응형

'Web Programming > php' 카테고리의 다른 글

php file_get_contents()  (0) 2018.09.05
php $_GET $_POST  (0) 2018.09.05
PHP 문자열 길이 (strlen, mb_strlen 함수)  (0) 2018.09.04
php data types  (0) 2018.09.04
php include, require  (0) 2018.09.03
728x90
반응형

strlen 함수

PHP 함수인 strlen 함수에 대해 php.net 에서는 아래와 같이 설명하고 있습니다.

문자열 길이를 얻습니다. 

그렇지만, 해당 함수는 영문 문자열이 몇 바이트(Byte)인지를 가져오는 함수입니다.
영문은 1 Byte로 계산하지만,
UTF-8 문서에 경우 각 문자를 1~4Byte까지 사용하므로, 한글은 한 글자당 1~4Byte에 길이가 반환되어 정확한 문자열의 길이를 알 수 없습니다.
그렇기 때문에 우리는 이를 해결 할 수 있는 함수인 mb_strlen 에 대해서도 함께 알아보겠습니다.

mb_strlen 함수

PHP 함수인 mb_strlen 은 php.net 에서 strlen과 동일한 설명을 가지고 있습니다.

문자열 길이를 얻습니다. 

그러면 무엇이 틀린 걸까요?
그건 바로 사용하는 인수 값이 틀립니다.

strlen은 문자열 하나만을 인수로 사용하지만,
mb_strlen은 문자열과 현재 파일의 인코딩(= 문자셋 혹은 charset )을 인수로 사용합니다.
mb_strlen 함수를 이용하면 한글 문자열 길이도 문제없이 가져올 수 있습니다.

▶사용법


강조 처리된 부분만 필수 입력 사항입니다.

strlen( 문자열 )


mb_strlen( 문자열 ,   인코딩 =  mb_internal_encoding() )


* 인코딩 : 값을 입력하지 않으면, 기본 문자 인코딩에서 사용되는 문자 인코딩을 가져옵니다. [참고 ]


▶strlen 함수 예제



코드

1
2
3
4
<?php
echo "영어 : ". strlen("Edward").'<br/>';
echo "한글 : ". strlen("반갑수");
?>


결과

영어 : 6
한글 : 9


▶mb_strlen 함수 예제


파일 인코딩이 UTF-8인 경우에 대한 예제입니다. (다른 인코딩이면 어딜 변경해야할지 아실거라 생각합니다.)

코드

1
2
3
4
<?php
echo "영어 : ". mb_strlen("Edward", "UTF-8").'<br/>';
echo "한글 : ". mb_strlen("반갑수", "UTF-8");
?>


결과

영어 : 6
한글 : 3



출처: http://extbrain.tistory.com/entry/PHP-문자열-길이-가져오기-strlen-mbstrlen-함수 [확장형 뇌 저장소]

728x90
반응형

'Web Programming > php' 카테고리의 다른 글

php $_GET $_POST  (0) 2018.09.05
php new line to br nl2br()  (0) 2018.09.05
php data types  (0) 2018.09.04
php include, require  (0) 2018.09.03
php 배열  (0) 2018.09.03
728x90
반응형

1. PHP Data Types

 PHP는 아래 데이터 타입을 지원합니다:

 - String

 - Integer

 - Float (floating point number - also called double)

 - Boolean

 - Array

 - Object

 - NULL

 - Resource



2. PHP String

 String은 문자의 연속입니다: "Hello World!" 같이

 

 String은 따옴표 안에 어떠한 텍스트든 존재합니다. 따옴표는 쌍따옴표든 사용가능 합니다:


1
2
3
4
5
6
7
8
<?php 
$x = "Hello world!";
$y = 'Hello world!';
 
echo $x;
echo "<br>"
echo $y;
?>
cs
 







3. PHP Integer

 Integer는 전체 숫자입니다. 숫자의 범위는 -2,147,483,648 ~ + 2,147,483,647 입니다.


 Integer의 규칙:

  - 적어도 한 숫자는 가져야 합니다

  - 콤마나 여백을 포함해선 안됩니다.

  - 소수점을 가져서는 안됩니다.

  - 양수나 음수를 가질 수 있습니다.

 



1
2
3
4
<?php 
$x = 5985;
var_dump($x);
?>
cs

 







4. PHP Float

 Float는 소수점을 갖는 숫자이거나 지수 형태의 숫자입니다.




1
2
3
4
<?php 
$x = 10.365;
var_dump($x);
?>
cs
 







5. PHP Boolean

 Boolean은 두 개의 상태를 표현합니다: TRUE 나 FALSE



$x = true;
$y = false;



 

 Boolean은 조건 테스팅에 사용됩니다. 




6. PHP Array

 Array는 단일 변수에 여러 값을 저장합니다.


 
1
2
3
4
<?php 
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
cs







7. PHP Object

 Object는 데이터가 저장되고 데이터가 어떻게 처리될지에 대한 정보를 갖는 데이터 타입입니다.


 PHP에서 Object는 명시적으로 선언해야 합니다.


 먼저, Object의 Class를 선언해야 합니다. 




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html>
<body>
 
<?php
class Car {
     function Car() {
         $this->model = "VW";
     }
}
// create an object
$herbie = new Car();
 
// show object properties
echo $herbie->model;
?>
 
</body>
</html>
cs

 


 




 







8. PHP NULL Value

 Null은 NULL 값만을 갖는 특별한 데이터 타입입니다.


 NULL인 데이터타입인 변수는 어떠한 값도 할당받지 않은 변수입니다.


 * 변수의 값 할당 없이 생성된 변수는 자동적으로 NULL 값을 할당합니다.



 
1
2
3
4
5
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
cs





9. PHP Resource

 특별한 Resource 타입은 사실상 데이터 타입이 아닙니다. Resource는 함수의 참조를 저장하거나 외부 자원을 저장합니다.


 resource 데이터 타입을 사용하는 일반적인 예제는 데이터 베이스 호출입니다.




출처: http://palpit.tistory.com/273 [palpit's log-b]

728x90
반응형

'Web Programming > php' 카테고리의 다른 글

php new line to br nl2br()  (0) 2018.09.05
PHP 문자열 길이 (strlen, mb_strlen 함수)  (0) 2018.09.04
php include, require  (0) 2018.09.03
php 배열  (0) 2018.09.03
php 함수 function  (0) 2018.09.03

+ Recent posts