grazzman Posted July 25, 2006 Share Posted July 25, 2006 I have a string that looks like this$string = "account=12345 name=grazzman"I need to then take $string and break it apart$account = "12345"$name = "grazzman"I have tried the split and mp_cut stuff, and it all returns a number or wants an array... Is their any way to do this without an array? I hate arrays. All I need is the name of the function that will do this without an array or a true/false/position respons... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/15600-problems-spliting-a-string/ Share on other sites More sharing options...
Ninjakreborn Posted July 25, 2006 Share Posted July 25, 2006 Look upsubstr() Quote Link to comment https://forums.phpfreaks.com/topic/15600-problems-spliting-a-string/#findComment-63455 Share on other sites More sharing options...
hackerkts Posted July 25, 2006 Share Posted July 25, 2006 You can also try explode(); Quote Link to comment https://forums.phpfreaks.com/topic/15600-problems-spliting-a-string/#findComment-63463 Share on other sites More sharing options...
effigy Posted July 25, 2006 Share Posted July 25, 2006 You need to make amends with arrays, and it would be better to use an associative one in this case.[code]<?php $string = "account=12345 name=grazzman"; $pieces = preg_split('/(?:=|\s+)/', $string); echo '<pre>', print_r($pieces, true), '</pre>'; $counter = 0; foreach ($pieces as $piece) { ### Evens are keys. if (!($counter % 2)) { $key = $piece; } ### Odds are values. else { $result[$key] = $piece; } ++$counter; } echo '<pre>', print_r($result, true), '</pre>';?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15600-problems-spliting-a-string/#findComment-63465 Share on other sites More sharing options...
PHPSpirit Posted July 25, 2006 Share Posted July 25, 2006 Take this:[code]<?php$string = "id=666 msg=getajob";list($a0, $a1, $b0, $b1) = split("[ =]", $string);$$a0 = $a1;$$b0 = $b1;echo $id.": ".$msg;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15600-problems-spliting-a-string/#findComment-63470 Share on other sites More sharing options...
grazzman Posted July 25, 2006 Author Share Posted July 25, 2006 I love this site....The one above this does not work, and effigy, Yes I do, but not today.... Here is my code right out of the app...[code] $test = ($session_chat[$sid]['visitor_name']); //test = Account-12345 Name-grazzman $pos = strpos($test, 'Account-'); // returns the start position for Account- $pos2 = strpos($test, 'Name-'); //returns the start position for Name- $test1 = substr($test, $pos, $pos2); //Returns "Account-12345 Name-grazzman" $test2 = substr($test, $pos2); //returns "Name-grazzman"[/code]The only problem with this is on $test1 is it not stopping when it hits $pos2 (Name-) it just goes to the end. From what I read on the php Manual page, it should be stopping when it hits the 2nd position string.This is from the php.net page. The 2nd line is what I'm after but it dosent work. Any thoughts...[code]<?phpecho substr('abcdef', 1); // bcdefecho substr('abcdef', 1, 3); // bcd?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15600-problems-spliting-a-string/#findComment-63497 Share on other sites More sharing options...
ryanlwh Posted July 25, 2006 Share Posted July 25, 2006 this may be better for you?[code]$string = "account=12345 name=grazzman";str_replace(' ','&',$string);parse_str($string);echo $account;echo $name;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15600-problems-spliting-a-string/#findComment-63504 Share on other sites More sharing options...
grazzman Posted July 25, 2006 Author Share Posted July 25, 2006 I'v decided to throw my monitor out the window.... This is turning into a script that should work, but isent....Main VARS are below, At the bottomg the javascript starts and I have added to this what ever field prints out... look for the PHP FREAKS STARTS HERE line near the bottom. I have really tried ever sugestion above and still will not pars this string. The only one that has come close is the substr functions, but it will not stop when it hits the 2nd position.... [code]<?php session_start() ; $session_chat = $_SESSION['session_chat'] ; $sid = ( isset( $_GET['sid'] ) ) ? $_GET['sid'] : "" ; $requestid = ( isset( $_GET['requestid'] ) ) ? $_GET['requestid'] : "" ; $sessionid = ( isset( $_GET['sessionid'] ) ) ? $_GET['sessionid'] : "" ; $userid = ( isset( $_GET['userid'] ) ) ? $_GET['userid'] : "" ; $action = ( isset( $_GET['action'] ) ) ? $_GET['action'] : "" ; $accountid = ( isset( $_GET['accountid'] ) ) ? $_GET['accountid'] : "" ; if ( !file_exists( "web/".$session_chat[$sid]['asp_login']."/".$session_chat[$sid]['asp_login']."-conf-init.php" ) || !file_exists( "web/conf-init.php" ) ) { print "<font color=\"#FF0000\">[Configuration Error: config files not found! -$sid] Exiting...</font>" ; exit ; } include_once("./web/conf-init.php") ; $DOCUMENT_ROOT = realpath( preg_replace( "/http:/", "", $DOCUMENT_ROOT ) ) ; include_once("$DOCUMENT_ROOT/web/".$session_chat[$sid]['asp_login']."/".$session_chat[$sid]['asp_login']."-conf-init.php") ; include_once("$DOCUMENT_ROOT/system.php") ; include_once("$DOCUMENT_ROOT/lang_packs/$LANG_PACK.php") ; include_once("$DOCUMENT_ROOT/API/sql.php") ; include_once("$DOCUMENT_ROOT/API/Chat/update.php") ; // set frame row properties depending if admin or regular request $frame_row_properties = "*,100%" ; if ( $session_chat[$sid]['isadmin'] && $session_chat[$sid]['deptid'] ) $frame_row_properties = "*,100%" ; // let's start the poll time $_SESSION['session_chat'][$sid]['admin_poll_time'] = time() ; $window_title = preg_replace( "/<(.*)>/", "", $session_chat[$sid]['visitor_name'] ) .": Support Request" ; // PHP FREAKS STARTS HERE // The visitor_name part holds the Account and Name ID's... This is being passed to mulitiple computers // dynamicly to start a chat session between 2 people. All I need is to pars the account and name into // different fields. $test = ($session_chat[$sid]['visitor_name']); // test equals "<o1153848487>Account=12345 Name=Some Name" $pos = strpos($test, 'Account='); $pos2 = strpos($test, 'Name='); $test1 = substr($test, $pos, $pos2); // Test1 equals "Account=12345 Name=Some Name" Takes the Session ID off $test2 = substr($test, $pos2); //Test2 equals "Name=Some Name" str_replace(' ','&',$test1); // test1 equals "Account=12345 Name=test" parse_str($string); ?><html><head><title> <?php echo $window_title ?> </title><script language="JavaScript"> alert(<?php echo $test2 ?>); // Name=Some Name alert(<?php echo $test1 ?>); //Account=12345 Name=Some Name alert(<?php echo $session_chat[$sid]['visitor_name'] ?>; //<o1153848487>Account=12345 Name=test alert(<?php echo $account ?>); //BLANK alert(<?php echo $name ?>); // BLANK</html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15600-problems-spliting-a-string/#findComment-63525 Share on other sites More sharing options...
ryanlwh Posted July 25, 2006 Share Posted July 25, 2006 the third argument for substr is LENGTH and not POSITION, that's why $pos2 won't work...i made some mistake with my code. below is the modified version[code]$test1 = preg_replace('/(Account=\d+)/','$1&',$test1);parse_str($test1);echo $Account;echo $Name;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15600-problems-spliting-a-string/#findComment-63534 Share on other sites More sharing options...
grazzman Posted July 25, 2006 Author Share Posted July 25, 2006 [size=15pt][color=red]THANK YOU THANK YOU THANK YOU[/color][/size]so, yea, that worked... Quote Link to comment https://forums.phpfreaks.com/topic/15600-problems-spliting-a-string/#findComment-63537 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.