Forums

[resolved] getting selected strings within another string (4 posts)

  1. fas.khan
    Member
    Posted 1 year ago #

    Hi,

    My Plugin is creating a string with the following value.
    :11:{i:0;s:14:"General public";i:1;s:24:"Educational institutions";i:2;s:16:"Large enterprise";i:3;s:15:"Local authority";i:4;s:15:"Local community";i:5;s:24:"Professional association";i:6;s:25:"Professional organization";i:7;s:20:"Research institution";i:8;s:30:"Small and Medium sized company";i:9;s:15:"Training center";i:10;s:30:"Young people between 15 and 25";}

    Now, I want only these values between the " ", which are for example:-

    - General public
    - Educational institutions
    - Large enterprise
    - ...

    So basically a function that would allow me to put the values between " and " into some array.

    Could you help me building a function in PHP like this? or may be there is some built in action which I cant find :(

    Many Thanks

  2. vtxyzzy
    Member
    Posted 1 year ago #

    What you are showing is a 'serialized' array. You can 'unserialize' it and list the entries in a loop. (You are missing the letter 'a' at the first of the string.)

    Assuming that the string is in the variable $list, this will unserialize it and list the items:

    $items = unserialize($list);
    echo '<ul>';
    foreach ($items as $item) {
       echo "<li>$item</li>";
    }
    echo '</ul>';
  3. fas.khan
    Member
    Posted 1 year ago #

    wow! thanks vtxyzzy

    I was just looking at the string and found a pattern of its appearence ...

    so i did this...

    $adf= (explode('"',$post->options));
    
            		    $i=0;
            		    $len = strlen($adf);
    
            		    for ($i = 0; $i < count($adf); $i++)
                                {
                                           if ( $i %2 != 0)
            			       	echo $adf[$i]."<br />";
            			   }

    It just gave me the values.

    Many Thanks again.

  4. vtxyzzy
    Member
    Posted 1 year ago #

    Glad you found a solution. Now, please use the dropdown at top right to mark this topic 'Resolved'.

Topic Closed

This topic has been closed to new replies.

About this Topic