var_export to string
Asked Answered
I

2

7

I have an HTML code in the $output string, this code is delimited by two terms: -startMiniPreview- and -endMiniPreview-. I need to take the part wrapped between those two terms and save it in another string variable. This is what I thought was the right way to do so, but it's not working at all. The HTMLCODE part is getting exported succesfully but not as a string

    $output="-startMiniPreview-HTMLCODE-endMiniPreview-EXTRA";
    preg_match( '/\-startMiniPreview-(.*?)\-endMiniPreview/', $output,$vt);
    $cutOut=strval(var_export($vt[1]));
Induline answered 28/11, 2012 at 18:32 Comment(0)
P
10

The documentation clearly gives the answer:

$cutOut = var_export($vt[1],true);

EDIT: That said, why are you doing this? Just $cutOut = $vt[1]; is all you need.

Peoples answered 28/11, 2012 at 18:33 Comment(1)
This actually worked. Silly me, I'm still a newb at PHP, but I just found out it's not complicated as I guessed.. thanks for the help!Induline
C
2

The second parameter to var_export makes it return the string instead of outputting it directly http://php.net/manual/en/function.var-export.php

mixed var_export ( mixed $expression [, bool $return = false ] )

So your code should look like

$cutOut=strval(var_export($vt[1]), TRUE);
Curtsey answered 28/11, 2012 at 18:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.