Page 1 of 1
replace numbers problem
Posted: Sat Aug 30, 2008 2:42 pm
by nicosns
Hello,
I have something like this
Code: Select all
str_replace("\n<font style=\x22font-size: ??px;\x22><a href=\x22http://www.domain.com/search.php?query=\x22></a></font></br> ", "", etc...
The problem is that I don't know wich number ?? wil be. But I know it has 2 digits. Now How can I tell PHP to replace the whole sentence even I don't know the numbers???
THX
Re: replace numbers problem
Posted: Sat Aug 30, 2008 8:46 pm
by marcth
Use a regular expression.
Re: replace numbers problem
Posted: Sat Aug 30, 2008 11:15 pm
by nicosns
marcth wrote:Use a regular expression.
Thanks for you answer, but I could not find a solution yet. I have tried different regex 'solutions', but it's not easy.
My script must do this:
I have a file with search actions from users. It collects information in this format, and it's for my eyes only,
Code: Select all
<font style="font-size: 20px;"><a href="http://www.domain.com/search.php?query=keyword">keyword</a></font></br>
<font style="font-size: 17px;"><a href="http://www.domain.com/search.php?query="></a></font></br>
<font style="font-size: 17px;"><a href="http://www.domain.com/search.php?query=keyword">keyword</a></font></br>
<font style="font-size: 20px;"><a href="http://www.domain.com/search.php?query="></a></font></br>
<font style="font-size: 16px;"><a href="http://www.domain.com/search.php?query=keyword">keyword</a></font></br>
<font style="font-size: 30px;"><a href="http://www.domain.com/search.php?query=keyword">keyword</a></font></br>
<font style="font-size: 14px;"><a href="http://www.domain.com/search.php?query="></a></font></br>
<font style="font-size: 10px;"><a href="http://www.domain.com/search.php?query=keyword">keyword</a></font></br>
<font style="font-size: 28px;"><a href="http://www.domain.com/search.php?query="></a></font></br>
You see that some lines contain incomplete information. So I have another script that filter those lines out (and also the duplicates), so I won't see empty lines in my html file. BUT the problem is that I use different font size, and I hope there is a easy way to tell php to just ignore those two digits (it's a number between 15-30) and find the dups and empty lines.
Example of my code,
Code: Select all
<?php
$page = "/home/website/domains/domain.com/public_html/outputdata.txt";
$nodups = array_unique (explode ("\n", str_replace("\n<font style=\x22font-size: 15px;\x22><a href=\x22http://www.domain.com/search.php?query=\x22></a></font></br> ", "", file_get_contents($page))));
echo implode("\n",array_slice($nodups,-101,100));
?>
You see '15' (px), but that 15 could also be '28'...
Anyone with a tip? thx!
Re: replace numbers problem
Posted: Sun Aug 31, 2008 10:27 am
by marcth
Here you go. The bill is in the mail!
Code: Select all
$input = array(
'<font style="font-size: 20px;"><a href="http://www.domain.com/search.php?query=keyword">keyword</a></font></br>',
'<font style="font-size: 17px;"><a href="http://www.domain.com/search.php?query="></a></font></br>',
'<font style="font-size: 17px;"><a href="http://www.domain.com/search.php?query=keyword">keyword</a></font></br>',
'<font style="font-size: 20px;"><a href="http://www.domain.com/search.php?query="></a></font></br>',
'<font style="font-size: 16px;"><a href="http://www.domain.com/search.php?query=keyword">keyword</a></font></br>',
'<font style="font-size: 30px;"><a href="http://www.domain.com/search.php?query=keyword">keyword</a></font></br>',
'<font style="font-size: 14px;"><a href="http://www.domain.com/search.php?query="></a></font></br>',
'<font style="font-size: 10px;"><a href="http://www.domain.com/search.php?query=keyword">keyword</a></font></br>',
'<font style="font-size: 28px;"><a href="http://www.domain.com/search.php?query="></a></font></br>',
);
$output = array();
foreach($input as $value) {
preg_match('/<a href=.*<\/a>/', $value, $results);
$output = array_merge($output, $results);
}
print '<pre>';
print_r($output);
print '</pre>';
Re: replace numbers problem
Posted: Sun Aug 31, 2008 10:53 am
by dude81
As far as I understood, the problem is to replace the font-size with a new one. If that is the case,one needs to use preg_replace
Code: Select all
$input = array(
'<font style="font-size: 20px;"><a href="http://www.domain.com/search.php?query=keyword">keyword</a></font></br>',
'<font style="font-size: 17px;"><a href="http://www.domain.com/search.php?query="></a></font></br>',
'<font style="font-size: 17px;"><a href="http://www.domain.com/search.php?query=keyword">keyword</a></font></br>',
'<font style="font-size: 20px;"><a href="http://www.domain.com/search.php?query="></a></font></br>',
'<font style="font-size: 16px;"><a href="http://www.domain.com/search.php?query=keyword">keyword</a></font></br>',
'<font style="font-size: 30px;"><a href="http://www.domain.com/search.php?query=keyword">keyword</a></font></br>',
'<font style="font-size: 14px;"><a href="http://www.domain.com/search.php?query="></a></font></br>',
'<font style="font-size: 10px;"><a href="http://www.domain.com/search.php?query=keyword">keyword</a></font></br>',
'<font style="font-size: 28px;"><a href="http://www.domain.com/search.php?query="></a></font></br>',
);
$output = array();
foreach($input as $value) {
$output[] = preg_replace('/d{2}/si',$yourfontsize,$value);
}