0

I am using jQuery jTable plugin to display a session variable data stored in my mysql db which will be in the format:

gateway|i:1;FirstName|s:7:"Shelley";LastName|s:2:"VS";Status|s:7:"Enabled";UID|s:10:"b81ef6a12a";AdminGroup|s:8:"Sysadmin";

I am displaying all the session data in a jtable column which is not required. I need to extract only the FirstName portion from the string using JavaScript. I am a javascript newbie. I did the following:

name: {
    title: 'Admin Name',
    display: function (data) {
        var x = data.record.data;
        if(x.match("FirstName")) {
            return "yes";
        }
    },
},

Which displays "yes" in my new jtable column as FirstName data is found in string using x.match. How can I extract only the first name portion from the above text using javascript. I will have multiple records in the same format.

Thanks in advance.

Added to my original post

@cytofu Excellent answer to a newbie..I changed the code to suit my jTable (without an additional function) as follows and it is running fine: Thank you.

firstname: {
title: 'First Name',                    
display: function (data) {
var str = data.record.data;
var startOfSection = str.indexOf('FirstName');
var startOfValue = str.indexOf('"',startOfSection)+1;
var endOffValue  = str.indexOf('"',startOfValue); //one char longer, as needed for   substring
var value = str.substring(startOfValue,endOffValue);
return value;
},
},

//last name

lastname: {
title: 'Last Name',                    
display: function (data) {
var str = data.record.data;
var startOfSection = str.indexOf('LastName');
var startOfValue = str.indexOf('"',startOfSection)+1;
var endOffValue  = str.indexOf('"',startOfValue); //one char longer, as needed for substring
var value = str.substring(startOfValue,endOffValue);
return value;
},
},
2
  • 3
    I don't recognise the format, what is it? Is there any particular reason not to use XML or JSON? Commented Nov 23, 2013 at 9:05
  • Rather then writing a custom parser, let's consider the source, that is if it is accessible. You wouldn't by chance be using PHP's serialize function, would you (it looks like you just swapped out the {} with |)? If this is the case, rather than serializing, consider encoding as JSON with json_encode since it can be easily decoded (parsed) in JS. Commented Nov 23, 2013 at 9:05

2 Answers 2

2

You have three common ways:

  • parse whole string to object. .split() is your best friend in this case;
  • change incoming format to JSON;
  • get your data with regex:

    var patient = 'gateway|i:1;FirstName|s:7:"Shelley";LastName|s:2:"VS";Status|s:7:"Enabled";UID|s:10:"b81ef6a12a";AdminGroup|s:8:"Sysadmin";';

    alert(patient.match(/FirstName\|\w\:\d+\:\"(\w+)\"/)[1]);

Sign up to request clarification or add additional context in comments.

1 Comment

You can add another regex with LastName in it, but in case you need more then two items from string, it's better to parse whole string and it's fields.
0

add this function to the script:

function extractValue(str,searchStr){

    var startOfSection = str.indexOf(searchStr);
    var startOfValue = str.indexOf('"',startOfSection)+1;
    var endOffValue  = str.indexOf('"',startOfValue); //Position of first char AFTER the value, as needed for substring

    var value = str.substring(startOfValue,endOffValue);

    return value;
}

you can test it with:

var str = 'gateway|i:1;FirstName|s:7:"Shelley";LastName|s:2:"VS";Status|s:7:"Enabled";UID|s:10:"b81ef6a12a";AdminGroup|s:8:"Sysadmin";';
var searchStr = 'FirstName|';

alert (extractValue(str,searchStr));

change your jTable function fragment as follows:

name: {
    title: 'Admin Name',
    display: function (data) {
        var x = data.record.data;
        if(x.match("FirstName")) {
            return extractValue(x,"FirstName");
        }
    },
},

fiddle: http://jsfiddle.net/nBG89/7/

11 Comments

Running it in fiddle produces no result ??
alert returns the result..But return ( firstName ); produces no output ??
it was logging the output to console, I changed it to alert the result instead. Also I removed the ; from the beginning of the searchstring, so that it can also be found if it is the first entry.
I would advise that you go trough some basic JS tutorials or get a text book! return is not a function, it is used in functions to return values.
I need to display the result and hence a return value is expected. Does not require an alert ?? The answer provided by user Tommi above is perfect. changed alert to return and i was able to produce the result..I am a javascript newbie as referred to in my earlier post...sorry for the silly questions.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.