[英]changing url pattern using regular expression in php
我想我对PHP完全不满意。
我正在尝试将网址格式更改为其他格式
从
至
http://www.example.co.kr/arti/politics/assembly/593397.html
因此,我只从原始网址中获取“ example0Bco0Bkr0Carti0Cpolitics0Cassembly0C5933970Bhtml”,然后对“ 0B”->“”进行了一些调整。 和'0C'->'/'
应当删除其他部分,例如da.feedsportal.com/c/34762/f/640634/s/2dd0817c/l/0L0S和/ia1.htm。
任何帮助将不胜感激。
只需使用urlencode
echo urlencode("http://www.example.co.kr/arti/politics/assembly/593397.html");
//=> http%3A%2F%2Fwww.example.co.kr%2Farti%2Fpolitics%2Fassembly%2F593397.html
那你就不用自己写
使用strpos , strrpos , substr和str_replace 。 我们将分四个主要步骤进行。 查找子字符串的开始和结束。 验证。 抓住子串。 转换子字符串。
基本上:
function convert ($inputString) {
$start = strpos( $inputString, '0L0S' );
$end = strrpos( $inputString, '/' );
if(! $start){
return false;
}
$start = $start + 4; // this is because strpos gives start of found string
$length = $end - $start;
$innerString = substr( $inputString, $start, $length);
$pass1 = str_replace ('0B', '.', $innerString);
$pass2 = str_replace ('0C', '/', $pass1);
return $pass2;
}
我敢肯定有一种很不错的方法。 但这应该可行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.