replace numbers problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nicosns
Forum Newbie
Posts: 21
Joined: Thu Jun 21, 2007 1:53 pm

replace numbers problem

Post 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
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: replace numbers problem

Post by marcth »

Use a regular expression.
nicosns
Forum Newbie
Posts: 21
Joined: Thu Jun 21, 2007 1:53 pm

Re: replace numbers problem

Post 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!
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: replace numbers problem

Post 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>';
 
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Re: replace numbers problem

Post 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);
 }
  
 
Post Reply