PHP preg_match URL replacement usage

2019-06-14 15:31发布

I have two URL patten structure in $url

  1. news.oxo.com/site/data/html_dir/2013/05/25/2013052500007.html
  2. salute.com/arti/society/health/588947.html

and want to change those to

  1. m.oxo.com/article.html?contid=2013052500007
  2. m.salute.com/article.html?contid=2013052500007

I have tried

<?php $url_m = preg_replace("salute.com","m.salute.com",$url); ?>
<?php echo $url_m; ?>

And I guess I am totally lost -_-;;;;

Any help might be highly appreciated.

2条回答
小情绪 Triste *
2楼-- · 2019-06-14 15:55

If you're responsible for both URLs and are using Apache you can handle the old addresses in the old Apache server with .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^news.oxo.com$
RewriteRule ^site/data/html_dir/[0-9]{4}/[0-9]{2}/[0-9]{2}/([0-9]+)\.html$ http://m.oxo.com/article.html?contid=$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^(www\.)?salute.com$
RewriteRule ^[^/]+/[^/]+/[^/]+/([0-9]+)\.html$ http://m.salute.com/article.html?contid=$1 [R=301,L]

If you're not responsible and are only wanting to rewrite the URLs in PHP (if you're changing a link from a database for example), you would do this:

<?php
$original_URLs[0] = "news.oxo.com/site/data/html_dir/2013/05/25/2013052500007.html";
$original_URLs[1] = "salute.com/arti/society/health/588947.html";

//patterns for m.oxo
$replacements[0] = 'm.oxo.com/article.html?contid=$1';
$patterns[0] = "!news.oxo.com/site/data/html_dir/[0-9]{4}/[0-9]{2}/[0-9]{2}/([0-9]+)\.html$!";

//patterns for m.salute 
$replacements[1] = 'm.salute.com/article.html?contid=$1';
$patterns[1] = '!salute\.com/[^/]+/[^/]+/[^/]+/([0-9]+)\.html$!';


foreach($original_URLs as $key=>$url){
    echo "<pre>".preg_replace($patterns,$replacements,$url)."</pre>";
}
?>

You can also replace the foreach with a string replacement as well if you're trying to replace links embedded in something like an article.

echo "preg_replace($patterns,$replacements,$some_article);
查看更多
Summer. ? 凉城
3楼-- · 2019-06-14 16:19

Thank you Absolute ZERO. I would like to multiply your ID with the infinite :)

I have tried your answer but it does not work. So I have made a little tweak on it. Before going on, I should clarify my situation a bit more to help for readers on this post.

I have a variable $urls in PHP. This is a link url for sending user to target page. In this variable, there are many patterns including those two:

news.oxo.com/site/data/html_dir/2013/05/25/2013052500007.html salute.com/arti/society/health/588947.html

With your answer and my tweak, I have managed to send users to mobile page.

$urls = get_post_link(~~~~~);
//patterns for m.oxo
$replacements[0] = 'm.oxo.com/article.html?contid=$1';
$patterns[0] = "!news.oxo.com/site/data/html_dir/[0-9]{4}/[0-9]{2}/[0-9]{2}/([0-9]+)\.html$!";

//patterns for m.salute 
$replacements[1] = 'm.salute.com/article.html?contid=$1';
$patterns[1] = '!salute\.com/[^/]+/[^/]+/[^/]+/([0-9]+)\.html$!';

$url = preg_replace($patterns,$replacements,$urls);

I have one more question in regard of this question. Perhaps it would be right to post it by opening another question but since new question is closely related to the old one, I put this on here.

New question is what replacement and patterns should be used to replace

from

kr.hello.feedsportal.com/c/34762/f/640634/s/2c6bcfa2/l/0L0Shani0Bco0Bkr0Carti0Cpolitics0Cpolitics0Igeneral0C589130A0Bhtml/story01.htm

to

www.hello.co.kr/arti/society/society_general/589159.html

Thanks you!

查看更多
登录 后发表回答