php code that will add spaces in desired positions of the string
I have entries like this:
username08-15-1008-31-1174.86.60.91http://sitename.com/04442
Where:
- username = username ;)
- 08-15-10 = year
- the rest of the numbers ( 08-31-1174.86.60.91 ) represents another date and ip which do no interest me
- the url of the site
- the first 0 zero after the url is the site's pagerank which can vary from 1 to lets say 7
- the second and third number 44, represents the number of internal links
- the fourth and fifth number represents the number of backlinks
What I am looking for is a php code that will add "space" after:
- the username;
- the date;
- the IP (just before http)
- after the url;
- after the first number;
- after the third number
With this spaces I can echo my code, copy paste it into excel and with the help of the spaces I can separate the data into columns. This way, I can manipulate it, lets say the oldest sites, with the greatest number of backlinks and the lowest number of outgoing links. etc.
I am thinking of something like this, but I do not know how to add the spaces:
<?
$sites = array(
'username08-15-1008-31-1174.86.60.91http://sitename.com/04442',
'username08-11-1009-05-1196.44.171.207http:开发者_开发百科//sitename.com/034'
);
foreach ($sites as $site)
{
// HERE WILL BE THE CODE
echo $site;
}
?>
Hope you can help! Ty!
Ok, if I understood correctly, this should do the trick for you:
for($i = 0; $i < count($sites); ++$i) {
$sites[$i] = preg_replace("/(\w*)(\d\d-\d\d-\d\d)\d\d-\d\d-\d\d([\d.]*)(.*\/)(\d)(\d\d)(\d*)/","$1 $2 $3 $4 $5 $6 $7",$sites[$i]);
echo $sites[$i];
}
Note that this assumes that your username consists of only letters, numbers or underscores, and that your URL will end with /.
You can use regex for that:
preg_match('/(\w+)(\d{2}-\d{2}-\d{4})-(\d{2}-\d{2})(\d+\.\d+\.\d+\.\d+)(.*)/',$string,$matches);
After this, just use $matches
as array, to extract each part of string. Concate with whatever spacers you like. $matches[1].' '.$matches[2].'etc'
精彩评论