1

I am trying to generate the array structure as coding style so it can be used for further development for that purpose i have used following:

function convertArray($string)
{
        $finalString = var_export($string, true);
        return stripslashes($finalString);
}

It worked fine but the problem is that it adds the additional quotes to the start and end of the value how can i remove these quotes.

Example generated string is as follows:

array (
  'foo' => 'array('foo','bar','baz')',
  'bar' => 'array('foo','bar')',
  'baz' => 'array('foo','bar')',
);

The string i need is:

array (
      'foo' => array('foo','bar','baz'),
      'bar' => array('foo','bar'),
      'baz' => array('foo','bar'),
    );

UPDATE

Here is how i create my array:

foreach( $attributes as $attrib )
    {
        if( $attrib->primary_key == '1' )
            $column[$attrib->name] = array("'$attrib->type'", "'$attrib->max_length'", '\'pk\'');
        else
            $column[$attrib->name] = array("'$attrib->type'", "'$attrib->max_length'");

        $string[$attrib->name] = 'array('.implode(',', $column[$attrib->name]).')';
    }

after processing from this loop the final array sent to the above function to convert it into my desired form/

4
  • I just ran your code on my PHP 5.3.10 - it doesn't add any of those quotes. Consider upgrade if you are using older versions. Commented Aug 12, 2012 at 19:23
  • 1
    you don't want a string, you want an array. Commented Aug 12, 2012 at 19:23
  • can you post what is in $string exactly that you passed in function? Commented Aug 12, 2012 at 19:44
  • i have updated my question if you want additional details about data refer to this question: stackoverflow.com/questions/11868245/… Commented Aug 13, 2012 at 6:19

2 Answers 2

1

And you can use backslashes

$string = "Some text \" I have a double quote";
$string1 = 'Second text \' and again i have quote in text';

And lost variant

You can use 1 idiot variant to create many lines string an in example:

$string = <<<HERE
Many many text
HERE;

But i dont recommend use this variant

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

Comments

0

try to use trim. Example:

$string = "'text with quotes'";
echo $string; // output: 'text with quotes'
echo trim($string, array("'")); // output: text with quotes

1 Comment

trim makes no difference to the output

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.