Converting urls/fields

 
Post new topic   Reply to topic    PHP User Group Malta Forum Index // PHP Projects
View previous topic :: View next topic  
Author Message
Christian Sciberras



Joined: 11 Jan 2008
Posts: 8
Location: Malta

PostPosted: Thu Apr 10, 2008 9:13 pm    Post subject: Converting urls/fields Reply with quote
Hello all!

I'm having some trouble with some php code.

My objective is having alot of html code in a string and making php change relative paths to absolute paths (the "extra" part of the absolute path is in a string).

Here's some pseudo code:
Code:
$code="<a href='http://example.com'><img src='img/img1.png'></a>";
$abs="http://test.com/";
process($src,$abs); // this is a custom function which does all the hard work (and is what I need to create!)
echo $src; // should read as:
// <a href='http://example.com'><img src='http://test.com/img/img1.png'></a>

The replacement/concatenating needs to be done for both src and href attributes and only when the url they point to is relative.

Thanks for reading this!

Kind regards,
Christian

Edit:
I tried this with str_ireplace() but there are several serious flaws:
'href="' could be written as 'href ="' or with more spaces
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
yancho
Site Admin


Joined: 13 Nov 2007
Posts: 56
Location: Iklin

PostPosted: Mon Apr 14, 2008 8:50 am    Post subject: Reply with quote

 So you want that your function replaces the href and src right? why not make process() output an array and :

php:

$code
='<a href="http://exa.com"><img src="img/img1.png"></a>'
$abs="http://tes.com/"
$result process($code,$abs); 
$str =  '<a href="'.$result[0].'"><img src="'.$result[1].'"></a>';
echo 
$str;

function 
process($code$abs) {
 
$res = array ();
 
$res[0] = $code;
 
$pos stripos($abs'http://'); 
 if (
$pos == false) { //meaning it is an absolute path
   
$newstring '<img src="'.$abs;
   
$res[1] = str_ireplace('<img src="/img/"',$newstring$code);
 }
 else { 
$res[1] = $abs; }
 return 
$res;
}  


Hope it helps Smile


_________________
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
Christian Sciberras



Joined: 11 Jan 2008
Posts: 8
Location: Malta

PostPosted: Thu Apr 17, 2008 8:14 pm    Post subject: Reply with quote

 Thanks yancho, but I tackled it differently. I'm using PEAR/POSIX regex.
Here's my code:
php:
// removes first slash
function rem_slash($txt){
    if(
$txt{0}=='/'){
        
$txt{0}='';
    }
    return 
$txt;
}
// make all action/href/src links pass through the proxy
function format($html,$ul){
    
$new='index.php?url=';
    
$ptcl=array('http:','https:','ftps:','ftp:','file:','mailto:','www.');
    
preg_match_all('/((action|href|src)+=[\'|"])([^\'"]+[^\'"]*)[\'|"]/i'$html$outPREG_SET_ORDER);
    foreach (
$out as $val) {
        
// val[0]="href='covac-software.com'" val[1]="href='" val[3]="covac-software.com"
        
$res=false;
        foreach (
$ptcl as $sr){ if(!(stripos($val[3],$sr)===false)){ $res=true; } }
        if(
$res){$html=str_replace($val[1].$val[3],$val[1].$new.rem_slash($val[3]),$html);}
            else{
$html=str_replace($val[1].$val[3],$val[1].$new.$ul.rem_slash($val[3]),$html);}
    }
    return 
$html;
}
// make sure it's a directory or domain
function dirfy($u){
    if(
strpos($u,':')===false){$u='http://'.$u.'/';}
    
$u=parse_url($u);
    
$p=pathinfo($u['path'],PATHINFO_DIRNAME);
    if(
$p!='\\'){
        return 
$u['scheme'].'://'.$u['host'].$p.'/';
    }else{
        return 
$u['scheme'].'://'.$u['host'].'/';
    }
}
?>

And it is used as follows:
php:
<?
$oldcode
="<a href='somewhere.com'>re</a><img src='/imgs/img.png'><img src='http://test.com/imgs/img.png'>";
$newcode=format($oldcode,dirfy($url));
This code will be the base code in a php proxy server project I'm working on called UPPS. (http://www.covac-software.com/proxy/).
The proxy is a solution to annoying school filters. I'm thinking in giving out the php source maybe. 
The special thing about it is that it includes a whole admin control panel and doesn't use local files,
so it can only lose bandwidth and not disk space.
Regards,
Christian

Back to top
View user's profile Send private message Visit poster's website MSN Messenger
yancho
Site Admin


Joined: 13 Nov 2007
Posts: 56
Location: Iklin

PostPosted: Mon Apr 21, 2008 6:22 pm    Post subject: Reply with quote
Glad u got it fixed Smile

Tried to keep things easy for u there heh Smile Remember that Regexp are far slower than normal (hard to play with) PHP functions - but I dont guess its an issue here.

Gluck!
_________________
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
Christian Sciberras



Joined: 11 Jan 2008
Posts: 8
Location: Malta

PostPosted: Wed Apr 23, 2008 3:43 pm    Post subject: Reply with quote
I guessed Regex would be slowwer. Well PHP is very fast, so it should got unnoticed. But, when perfecting the proxy, I think it would be better to create a custom function.
Thanks,
Christian
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Post new topic   Reply to topic    PHP User Group Malta Forum Index // PHP Projects All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

alexisRed v1.2 // Theme Created By: Andrew Charron and Web Hosting Bluebook // Icons in Part By: Travis Carden
Boards optimised using the phpBB-SEO mod found at phpbb-seo.com
Boards hosted courtesy of solutions-lab.net
Link Backs : PHPClasses.org - MT Page :: PHPUsergroups.org - MT Page



Powered by phpBB © 2001, 2002 phpBB Group