静态化 - 伪静态技术(PHP正则表达式实现)

本文介绍了一个使用PHP实现的伪静态处理方法。通过正则表达式匹配URL参数,并将其解析为可处理的数据结构。该方法可以将动态页面的URL转换为类似静态页面的形式,有助于SEO优化。

效果:

 

代码:

<?php
// + —— —— —— —— —— —— ——  —— —— —— —— —— —— —— —— ——  —— —— —— —— —— —— 
// + 正则表达式,实现伪静态处理
// + url:http://localhost/forgestatic/index.php/news-class12-id43.html
// + —— —— —— —— —— —— ——  —— —— —— —— —— —— —— —— ——  —— —— —— —— —— —— 

//设置输出编码
header('Content-Type:text/html; charset=utf8');

//获取参数信息
//参数格式为:'/news-class12-id43.html'
$_urlParam = $_SERVER['PATH_INFO'];

// 匹配模式
// 匹配后格式:
// Array
// (
//     [0] => /news-class12-id43.html
//     [1] => news
//     [2] => class12
//     [3] => class
//     [4] => 12
//     [5] => id43
//     [6] => id
//     [7] => 43
// )
$_pattern  = '#^/([a-z]+)-(([a-z]+)([0-9]+))-(([a-z]+)([0-9]+))\.html$#';

//接收数据参数
$_paramArr = array();

//匹配获取
if ( preg_match($_pattern, $_urlParam, $_paramArr) ) {
	//打印数据
	echo '<pre>';
	print_r($_paramArr);
	echo '</pre>';
} else {
	exit('参数格式异常!');
}

 

转载于:https://www.cnblogs.com/KTblog/p/5166392.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值