[英]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.