Help with a Preg_Replace.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • minimatrix
    New Member
    • Nov 2008
    • 5

    Help with a Preg_Replace.

    Hi, I am quite new to php but picking it up quite quickly, however I dont understand how a preg replace works. I am trying to count the number of columns selected but when I do the count I sometimes get +1 than what it should be.

    e.g. I have in my array A,B,C the count would return 3 which is correct

    but I have my data like this A,B,C, this adds 1 to the count because there is a comma on the end. I jus need to know how to do a preg_repace

    here is a snippet from my code:

    Code:
    	// Store selected list of columns in session
    	$_SESSION[$settingname]=$settings;
    
    	// Write the session to SETTINGS / SETTINGSVALUES
    	// ( default NAME='.userid' ie. '.coling'
    	$settingname=".".$_SESSION['USERID'];
    	writeSettings( array( 'name'=>$settingname, tabs=>'all' ) );
    
    	$sets=split(",",$settings);
    	
    preg_replace("/,/", "{COMMA}", $settings);       
    	$num=count($sets);
    
    
    
    	exit_error( "<center> $num Columns Saved successfully,<br><br> 
    		
    		
    		<input type='Button' class='submit' value='Close' onClick=\"javascript:window.close();\" >
    		</center>	
    		" );
    As you can see I have no idea how to do a preg_replace, so would any body be able to help me sort this and possibly explain in laymans terms how a preg_replace works?


    Thanks in advance.
    Last edited by Markus; Dec 5 '08, 12:53 PM. Reason: corrected code tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    for a simple comma replacement you'd better use PHP: str_replace(). if you don't want the comma at the end of your data string, shorten (remove the last comma) the string first (PHP: substr()).

    and one more thing:

    For the benefit of our experts and yourself, it is a posting guidelines that all users use [code] tags when posting code. This makes the code easier to read and, in turn, helps our experts answer your questions.

    An easy way to do this is to highlight the code in the textarea and then hit the '#' button at the top of the textarea.

    Please read the Posting Guidlines so you're more familiar with how things work.

    regards
    Last edited by Dormilich; Dec 5 '08, 10:26 AM. Reason: added [CODE] info

    Comment

    Working...