-1

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
  • What are you lost about? What is your question? When you say you have 2 "pattern structures" in $url, is $url an array? If $url is an array, preg_replace() will return an array. Try debugging. Maybe using var_dump() will help. Commented May 25, 2013 at 4:07
  • I mean it is not working and I do not really understand how to do it. $url is not an array. I could store no.1 pattern or no.2 pattern as RSS feed link provides. I want to replace that link address to mobile address. :) Commented May 25, 2013 at 4:22

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.