Jump to content

[SOLVED] Removing certain characters


rhyspaterson

Recommended Posts

Hey guys,

 

Have a bit of a problem, and am quite unsure of how to fix it. I have a variable that is returned to me that looks like this:

 

Policy=frank; IP Address=10.10.10.10

 

I want to strip the string and turn it into an array so that i have only 'frank' and '10.10.10.10' (array[0] and array[1] respectively).

 

Any suggestions my elite coders?

 

Cheers!  ;D

Link to comment
https://forums.phpfreaks.com/topic/79038-solved-removing-certain-characters/
Share on other sites

Thanks for your reply!

 

Unfortunately i think i may have not been clear enough. I have a single variable with multiple bits of information within it.

 

var foo = 'Policy=frank; IP Address=10.10.10.10';

 

I would like to strip the foo variable of everything except 'frank' and '10.10.10.10' and split it into an array. Please note that 'frank' could be any name and '10.10.10.10' could be any IP address. I was thinking of something that would search for the = signs, capture everything after it until it reaches a space, then looked for the next = sign and do the same... but i have no idea how to go about it.

<html>

    <script language="javascript">

        var str = "Policy=frank; IP Address=10.10.10.10";

 

        var mixed_array = new Array();

        var tmp_arr = new Array();

        var arr = new Array();

        mixed_array = str.split(';');

        for(x=0; x<mixed_array.length; x++) {

          tmp_arr = mixed_array[x].split('=');

          arr[x] = tmp_arr[1];

        }

 

        for(x=0; x<arr.length; x++) {

          document.write("array[" + x + "] -- " + arr[x]+"<br>");

        }

    </script>

</html>

 

Thanks for that!

 

Turned it into a function and changed a few things:

 

/*
* Clean up the string
*/	
   splitStringToArray : function ( string ) {	

var string = dController.policy;
var mixedArray = new Array();
var temporaryArray = new Array();	
var array = new Array();	

mixedArray = string.split(';');

for(x = 0; x < mixedArray.length; x++) {
   temporaryArray = mixedArray[x].split('=');
   array[x] = temporaryArray[1];	   
   }
return array;
}
   ,

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.