Jump to content

Problems spliting a string


grazzman

Recommended Posts

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
Link to comment
Share on other sites

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] 
Link to comment
Share on other sites

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

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

?>
[/code]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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