2

Is it possible to have a string like this:

|
details==Here are some details
|
facebook_url==therweerw
|
random_word==blah blah
|

and get this:

$details = "Here are some details";
$facebook_url = "therweerw";
$random_word = "blah blah";

The main point is I'd like to parse this in a way that the string to the left of the "==" delimiter will become a variable with the string to the right as it's string. I'd like to not have to hard-code those variables.

2
  • explode won't be a good option, at least not by it self. also you have a closing quote missing in details Commented Jun 17, 2011 at 6:13
  • I would love to see this working :-). To understand this, are you trying to generate any PHP code? Or you want to have this as a part of a code which does something. Commented Jun 17, 2011 at 6:14

3 Answers 3

5
$str = '|
details==Here are some details
|
facebook_url==therweerw
|
random_word==blah blah
|';

preg_match_all('~^(\w+)==(.*)$~m', $str, $matches);

foreach ($matches[1] as $i => $name) {
    $$name = $matches[2][$i];
}

var_dump($details, $facebook_url, $random_word);

And running sample: http://ideone.com/Vns7Y

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

4 Comments

I mean, what more could you want?
@Doug Owings: uhm, what? I want million dollars ;-)
Just saying: it took you four minutes to post a working sample. Impressive.
@Doug Owings: hehe, that wasn't so obvious for me from your first phrase ;-))
1

You can use a combination of explode() plus extract() to do this.

4 Comments

How can extract() help with this?
extract() is another way of doing the equivalent of $$x = y; en masse when you have an associative array where the keys are x's and the values are y's.
I know what extract() does. I'm asking how can extract() be useful, when explode() creates an array with numeric indexes. Do you really suggest to use loop to create an array with variable names as keys and then use extract() instead of creating variables directly in the same loop?
No? I agree that using variable variables is probably the more straightforward way to do it, and would never argue differently; extract() was simply the first thing that came to mind, and there wasn't much of a point adding the other when someone had already added it as an answer.
1

I would probably use preg_match_all() for this, but here's example with explode():

$str = "|
details==Here are some details
|
facebook_url==therweerw
|
random_word==blah blah
|";
$str = str_replace("\r", '', $str);
$str = trim($str, "|\r\n");
foreach ( explode("\n|\n", $str) as $line ) {
    $line = trim($line);
    list($varName, $varValue) = explode('==', $line);
    ${$varName} = $varValue;
}
var_dump($details, $facebook_url, $random_word);

Output:

string 'Here are some details' (length=21)
string 'therweerw' (length=9)
string 'blah blah' (length=9)

Comments

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.