Current location: Hot Scripts Forums » Programming Languages » PHP » Replacing every second instance in a string?


Replacing every second instance in a string?

Reply
  #1 (permalink)  
Old 10-19-05, 11:18 AM
kdlklm kdlklm is offline
Newbie Coder
 
Join Date: Jul 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Replacing every second instance in a string?

I'm relatively inexperienced, but I can't seem to figure out how to replace every second instance in a string. For example:

<tr> with <tr class="Body2">

Any information would be extremely helpful.
Reply With Quote
  #2 (permalink)  
Old 10-19-05, 11:23 AM
Richard's Avatar
Richard Richard is offline
Wannabe Coder
 
Join Date: Sep 2005
Location: Oxford, UK
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
If you want to replace every 'instance' in a string, you can use the str_replace function
PHP Code:

$String str_replace("<tr>""<tr class=\"Body2\">"$String); 

This will replace all "<tr>" with "<tr class="Body2">" in $String.
Reply With Quote
  #3 (permalink)  
Old 10-19-05, 11:39 AM
kdlklm kdlklm is offline
Newbie Coder
 
Join Date: Jul 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
I want to be able to change every second instance. Not every instance.
Reply With Quote
  #4 (permalink)  
Old 10-19-05, 11:51 AM
Richard's Avatar
Richard Richard is offline
Wannabe Coder
 
Join Date: Sep 2005
Location: Oxford, UK
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, nps. What I would personally recommend is looping through the string, then when an instance is found, a variable is changed, depending on the variable, the instance will/won't be changed, like so:
PHP Code:

$instance 1;

for (
$i=0$i<=strlen($string); $i++) {
    if (
substr($string$i4) == "<tr>") {
        if (
$instance == 0) {
            
substr_replace($string"<tr class=\"Body2\">"$i);
        }
        
$instance++;
    }

This will change all 'even' instances, basically every second instance.
Reply With Quote
  #5 (permalink)  
Old 10-19-05, 12:13 PM
kdlklm kdlklm is offline
Newbie Coder
 
Join Date: Jul 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
not quite sure why it's not working for me. But this is what I'm using.

PHP Code:

function update_content_CHL(&$CHL_content) {    // use this to modify the HTML tags etc

    
$CHL_content str_replace('<TABLE>','<TABLE Width="100%">',$CHL_content);
    
$CHL_content str_replace(' in Rankings','',$CHL_content);
    
$CHL_content str_replace('Rank','R',$CHL_content);
    
$CHL_content str_replace('(','<br>(',$CHL_content);
    
$CHL_content str_replace(' Week','',$CHL_content);
    
//$CHL_content = str_replace('<TR>','<TR class="Body2">',$CHL_content);
 
$instance 1
for (
$i=0$i<=strlen($CHL_content); $i++) { 
    if (
substr($CHL_content$i4) == "<TR>") { 
        if (
$instance == 0) { 
            
substr_replace($CHL_content"<tr class=\"Body2\">"$i); 
        } 
        
$instance++; 
    } 

Reply With Quote
  #6 (permalink)  
Old 10-19-05, 01:47 PM
Richard's Avatar
Richard Richard is offline
Wannabe Coder
 
Join Date: Sep 2005
Location: Oxford, UK
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Think I missed the last INT in the substr_replace function, try:
PHP Code:

function update_content_CHL(&$CHL_content) {    // use this to modify the HTML tags etc

    
$CHL_content str_replace('<TABLE>','<TABLE Width="100%">',$CHL_content);
    
$CHL_content str_replace(' in Rankings','',$CHL_content);
    
$CHL_content str_replace('Rank','R',$CHL_content);
    
$CHL_content str_replace('(','<br>(',$CHL_content);
    
$CHL_content str_replace(' Week','',$CHL_content);
    
//$CHL_content = str_replace('<TR>','<TR class="Body2">',$CHL_content);
 
$instance 1
for (
$i=0$i<=strlen($CHL_content); $i++) { 
    if (
substr($CHL_content$i4) == "<TR>") { 
        if (
$instance == 0) { 
            
substr_replace($CHL_content"<tr class=\"Body2\">"$i4); 
        } 
        
$instance++; 
    } 

Reply With Quote
  #7 (permalink)  
Old 10-19-05, 02:19 PM
kdlklm kdlklm is offline
Newbie Coder
 
Join Date: Jul 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
still no go. I will post the whole file this time.

PHP Code:

<?


$url 
"http://www.chl.ca/CHL/topten.html"// Trailing slash when not using filename 
$file "includes/cache/CHL_ranking_cache.html"// file to write to. remember to chmod 777 to not get errors 
$unique_start '<TD VALIGN=TOP><div id=smtext>Rank</TD>'// Where to begin to grab 
$unique_end '<br><br><b>CHL WEEKLY TOP TENS</b><br>'// Where to end the grab 
//$cache_tolerance = 43200;  // How many seconds old the cache file can get  Set at 12 hours
$cache_tolerance 1;  // How many seconds old the cache file can get  Set at 1 second

//-----------------------------------------------------------

 
function update_content_CHL(&$CHL_content) {    // use this to modify the HTML tags etc 
    
$CHL_content str_replace('<TABLE>','<TABLE Width="100%">',$CHL_content); 
    
$CHL_content str_replace(' in Rankings','',$CHL_content); 
    
$CHL_content str_replace('Rank','R',$CHL_content); 
    
$CHL_content str_replace('(','<br>(',$CHL_content); 
    
$CHL_content str_replace(' Week','',$CHL_content); 
    
//$CHL_content = str_replace('<TR>','<TR class="Body2">',$CHL_content); 
 
$instance 1;  
for (
$i=0$i<=strlen($CHL_content); $i++) {  
    if (
substr($CHL_content$i4) == "<TR>") {  
        if (
$instance == 0) {  
            
substr_replace($CHL_content"<tr class=\"Body2\">"$i4);  
        }  
        
$instance++;  
    }  


    
// use any replacement routines in here.
}

//-----------------------------------------------------------

function write_cache_CHL($filename) {    // outputs the cached file
    // echo 'writing from cache<br>';
    
$CHL_contents implode("",@file$filename ) ); 
    
update_content_CHL ($CHL_contents);
    echo 
$CHL_contents;
}

//-----------------------------------------------------------

function check_domain_CHL($target) {    // tests if domain is accessible by opening a socket to it
    
$fetch_domain parse_url($target); 
    
$fetch_domain $fetch_domain[host]; 

    
$socket_handle fsockopen("$fetch_domain"80$error_nr$error_txt,30); 
    if(!
$socket_handle
    { 
        echo 
$target ' could not be reached.<br>';
        return 
"false";
    }
    return 
"true";
// function check_domain_CHL

//-------------------------------------------------------------

function update_cache_CHL($url,$unique_start,$unique_end,$file) {
      
    
// echo 'updating cache<br>';
    
if (check_domain_CHL($url)=='true') {    // only update if we find the domain
    
        
$handle fopen ("$url""rb");
        
$fd "";
        do {
            
$data fread($handle8192);
            if (
strlen($data) == 0) {
                break;
                }
            
$fd .= $data;
        } while(
true);
        
fclose ($handle);

        if (
$fd
        {
            
$startstrpos($fd"$unique_start"); 
            
/* echo "start found at " . $start . "<br>"; */
            
$finishstrpos($fd"$unique_end"); 
            
/* echo "finish found at " . $finish . "<br>"; */
            
$length$finish $start 0;
            
$code=Substr($fd$start$length);
            
$code strip_tags($code'<tr><td><table>');
            
$code'<TABLE width="100%" cellspacing="0" cellpadding="0" border="0"><TR bgcolor="#d8d8c4">'.$code."<CENTER>Updated: " date ("F dS Y h:i A") . "</CENTER>";
        } 
    
    
// output to cache file
    
$tmpfile fopen($file,"w+"); 
    
$fp fwrite($tmpfile,$code); 
    
fclose($tmpfile); 
    
flush (); 
    
    }

}

//--------------Main section starts here-------------------------------

ini_set('max_execution_time''0'); 
flush (); 

// check how old the cache file is
if (file_exists($file)) {
    
clearstatcache();  // filemtime info gets cached so we must ensure that the cache is empty
    
$time_difference time() - filemtime($file);
} else {
    
$time_difference $cache_tolerance;  // force update
}

if  (
$time_difference >= $cache_tolerance){    // update the cache if need be
    
update_cache_CHL($url,$unique_start,$unique_end,$file);
}
write_cache_CHL($file);    // we always only output from cache


?>
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help to turn a filelist in to a winamp playlist mike-wigan Windows .NET Programming 0 10-18-05 11:10 AM
ASP syntax error stuckonaproject ASP 8 12-14-04 10:30 AM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM
replacing certain parts of certain strings in text files Archbob PHP 0 07-29-03 04:23 PM


All times are GMT -5. The time now is 06:17 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.