Hi You, I am Tade

You are here: Home / Lập trình / Dùng hàm preg_match lấy html từ website khác

Dùng hàm preg_match lấy html từ website khác

19 December, 2015 by Hoàng Thức

Chúng ta sẽ kết hợp với 1 hàm preg_match với hàm file_get_contents. Công dụng 2 hàm này sẽ được sử dụng như sau:

file_get_contents : Lấy dữ liệu từ website.
preg_match: Lọc html cần lấy.
Đầu tiên cần chọn trang đối tượng cần lấy. Ở đây mình sẽ lấy tỷ giá usd – tỷ giá ngoại tệ từ website 24h.com.vn link : http://www.24h.com.vn/ttcb/tygia/tygia.php


<?php
$link = "http://hcm.24h.com.vn/ttcb/tygia/tygia.php";
$html = file_get_contents($link);

$reg = '/class="tb-giaVang">(.*?)</table>/s';
$match = array();
$result = preg_match($reg, $html, $match);            
?>

Đoạn code hiển thị


     <div class="div_Wrapper">
            <h1>Tỷ giá cập nhật mới nhất</h1>
              <table class="tbl_TyGia" cellpadding="0" cellspacing="0" width="100%">
               <?php _e($match[1]); ?>
           </table>
        </div>

Thêm đoạn css cho sinh động :))


<link rel='stylesheet'  href='http://www.24h.com.vn/css/ty_gia-2014.css?v=20140916?ver=20150912' type='text/css' media='all' />          


Filed Under: Lập trình, Php Tagged With: Php

Comments

  1. toeic says

    16 April, 2016 at 8:02 pm

    I am really happy to read this web site posts which includes plenty of valuable facts, thanks for providing these data.

  2. eebest8 says

    8 July, 2016 at 1:18 pm

    “Hmm is anyone else encountering problems with the pictures on this blog loading? I’m trying to find out if its a problem on my end or if it’s the blog. Any feed-back would be greatly appreciated.”

  • Home
  • Về tôi
  • Liên hệ

Danh mục

  • Blog
  • Lập trình
  • Laravel
  • MYSQL
  • Php
  • Thủ thuật máy tính
  • Vps
    • DirectAdmin
  • Wordpress
    • Plugin wordpress
    • Wordpress căn bản
    • Wordpress nâng cao
      • Custom post wordpress

Archives

  • October 2021
  • January 2021
  • December 2020
  • November 2020
  • September 2020
  • August 2020
  • September 2019
  • August 2019
  • July 2019
  • June 2018
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • March 2017
  • November 2016
  • September 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • January 2016
  • December 2015
  • May 2015
  • April 2015
Copyright © 2023 · HoangThuc.com · Genesis Framework