Forum Moderators: coopster
for example With these urls (http:// removed to break link but would exist in text)
www.<someurl>.com/money/es_MX/
www.<someurl>.com/money/de/
www.<someurl>.com/money/fr/
www.<someurl>.com/money/Coins-and-Paper-Money/Coins-Ancient/es_MX/
would become
www.<someurl>.com/money
www.<someurl>.com/money
www.<someurl>.com/money
www.<someurl>.com/money/Coins-and-Paper-Money/Coins-Ancient
Other links would not change
www.<someurl>.com/paper/blah/de/
www.<someurl>.com/paper/blah/fr/
www.<someurl>.com/paper/blah/es_MX/
there is a series of 6 subdirectories that need url fixing... Lets call them dir1...dir6
and a series of 32 possible /xx[xx]/ link ending that need removeing...
Any help is appreciated.
Well, assuming the strings don't appear anywhere else in the URLs then you can create an array of the subdirectories you want to remove, then replace those with nothing. For example:
$remove = array(
'/es_MX/',
'/de/',
'/fr/',
'etc.....'
);
# then replace them
$html = str_replace($remove, '', $html);
Try that and see where it gets you.
You could set the sub directories you want addressed in an array, apply each value to your pattern in your loop iteration and execute your preg_replace after the pattern has been adjusted. In pseudocode, something like this ...
$subs = array('money', 'anotherdir', 'nextdir', 'lastdir');
foreach ($subs as $sub) {
$pattern = "/pattern here utilizing your '$sub' variable/";
$subject = preg_replace($pattern, $replacement, $subject);
}
<?php
$subs = "dir1圬ir2圬ir3圬ir4圬ir5圬ir6";
$domain = "somedomain";
$langs = "de如t如t_BR妄o圯s圯s_MX圩r夷t夸a屹h屹h_TW地r好l圯l字u好o圯n在g多r圭s圬a圩i多u夷s宇l如l字o存r存l圭y宇r奸a存v";
$src = fopen("src.txt","r");
$dest = fopen("dest.txt","w");
while (!feof($src))
{
$data = fgets($src);
fwrite($dest,preg_replace("#(http://www\.".domain."\.com/(?:".subs.")(?:/[^/]+)*)/(".langs.")/#","\1",$data));
}
fclose($src);
fclose($dest);
?>
Sometimes the language directories are valid and desired. So that solution wont work...
@coopster
The pattern I used in the code above came from a regular expression that found all the patterns and stored in the first reference the new value in EditPadPro... I think I am just doing something wrong in php code or php regular expression syntax...
Also if I remove the # for the pattern I get a / is an unknown modifier
Perl compatible regular expressions are contained within delimiters [php.net]. You are using the pound sign (#) as the delimiter and as soon as you remove it you are going to get the error because a delimiter is required.