Reputation: 1330
I would like to transfer a specific link to another. The patterns looks like this:
http://domain.com/sub/code/name
to http://a.domain.com/subs/code/name
I need a
one letter prefix before domain, the sub
will be plural subs
and thats all, all the rest needs to be equal.
Of course the link can start with http://
, www
, http://www
Some mix of substr_replace
, strpos
and explode
, I stucked more than 1 hour on this task, decided to ask it.
Upvotes: 0
Views: 72
Reputation: 4738
Try this:
<?php
function convertLink($str)
{
//ensure a scheme exists
if(strpos($str,'http://')===false&&strpos($str,'https://')===false)
{
$str='http://'.$str;
}
$parts=parse_url($str);
$scheme=$parts['scheme'];
$domain=$parts['host'];
$path=array_key_exists('path',$parts)?$parts['path']:'';
$query=array_key_exists('query',$parts)?('?'.$parts['query']):'';
$fragment=array_key_exists('fragment',$parts)?('#'.$parts['fragment']):'';
if(strpos($parts['host'],'www.')===0){$domain=substr($parts['host'],4);}
if(array_key_exists('port',$parts)){$domain.=':'.$parts['port'];}
if(strpos($path,'/sub/')===0){$path='/subs/'.substr($path,5);}
return "{$scheme}://a.{$domain}{$path}{$query}{$fragment}";
}
$links=array();
$links[]='domain.com/sub/code/name';
$links[]='domain.com:20/sub/code/name';
$links[]='www.domain.com/sub/code/name';
$links[]='http://domain.com/sub/code/name';
$links[]='http://www.domain.com/sub/code/name';
$links[]='https://domain.com/sub/code/name';
$links[]='sub.domain.com/sub/code/name';
$links[]='domain.com/sub/code/name?a=1&b=2#c';
echo '<pre>';
foreach($links as $link)
{
echo str_pad($link, 40,' ',STR_PAD_RIGHT).'=> '.convertLink($link)."\n";
}
echo '</pre>';
Output:
domain.com/sub/code/name => http://a.domain.com/subs/code/name domain.com:20/sub/code/name => http://a.domain.com:20/subs/code/name www.domain.com/sub/code/name => http://a.domain.com/subs/code/name http://domain.com/sub/code/name => http://a.domain.com/subs/code/name http://www.domain.com/sub/code/name => http://a.domain.com/subs/code/name https://domain.com/sub/code/name => https://a.domain.com/subs/code/name sub.domain.com/sub/code/name => http://a.sub.domain.com/subs/code/name domain.com/sub/code/name?a=1&b=2#c => http://a.domain.com/subs/code/name?a=1&b=2#c
Upvotes: 0
Reputation: 9795
What you are asking is fairly vague so I assume by "transfer" you mean "replace with". If that is the case, then you should look into Regular Expressions (or Regex).
Regular expressions are relatively universal, so when you learn them once, you can apply them with most languages.
In PHP, you can perform a Regex Replace with preg_replace
, but it may help to read up a bit first on the different elements of patterns.
Relevant Links:
EDIT: The end result you are after can still be achieved using strpos
, explode
etc as you described, but Regexs will likely get you there faster.
Upvotes: 1
Reputation:
Try something like this:
/^((?:http://)|(?:http://www\.)|(?:www\.))(\w+)(\.\w+\/)([\w+_\-]+)(\/.*?)$/
#$1YOUR_STRING.$2$3SOMETHING_ELSE$5
RegExr example: http://regexr.com?38dn0
Upvotes: 1