Jump to content


Photo

Problems spliting a string


  • Please log in to reply
9 replies to this topic

#1 grazzman

grazzman
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 25 July 2006 - 03:57 PM

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

#2 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 25 July 2006 - 03:59 PM

Look up
substr()

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#3 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 25 July 2006 - 04:11 PM

You can also try explode();

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 25 July 2006 - 04:12 PM

You need to make amends with arrays, and it would be better to use an associative one in this case.

<?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>';
?>

 
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 PHPSpirit

PHPSpirit
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 25 July 2006 - 04:18 PM


Take this:

<?php

$string = "id=666 msg=getajob";

list($a0, $a1, $b0, $b1) = split("[ =]", $string);

$$a0 = $a1;
$$b0 = $b1;

echo $id.": ".$msg;

?>


#6 grazzman

grazzman
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 25 July 2006 - 05:03 PM

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...

        $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"

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...
<?php

echo substr('abcdef', 1);    // bcdef
echo substr('abcdef', 1, 3);  // bcd

?>


#7 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 25 July 2006 - 05:13 PM

this may be better for you?
$string = "account=12345 name=grazzman";
str_replace(' ','&',$string);
parse_str($string);

echo $account;
echo $name;

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#8 grazzman

grazzman
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 25 July 2006 - 05:40 PM

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....

<?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>


#9 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 25 July 2006 - 05:51 PM

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

$test1 = preg_replace('/(Account=\d+)/','$1&',$test1);
parse_str($test1);
echo $Account;
echo $Name;

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#10 grazzman

grazzman
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 25 July 2006 - 05:55 PM

THANK YOU THANK YOU THANK YOU

so, yea, that worked...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users