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/

share|improve this question

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. – Tim yesterday
1  
you don't want a string, you want an array. – Kalpesh Mehta yesterday
can you post what is in $string exactly that you passed in function? – Kalpesh Mehta yesterday
i have updated my question if you want additional details about data refer to this question: stackoverflow.com/questions/11868245/… – Shayan Husaini 14 hours ago
feedback

2 Answers

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

share|improve this answer
Please see the update – Shayan Husaini 14 hours ago
feedback

try to use trim. Example:

$string = "'text with quotes'";
echo $string; // output: 'text with quotes'
echo trim($string, array("'")); // output: text with quotes
share|improve this answer
trim makes no difference to the output – Shayan Husaini yesterday
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.