0

I have a lot data into one string and i want to get some particular chunks of data.

Example of my data:

lorem ipsum fashjdf hsakj sdhsdaf something: 'data' jhjkhf akjh fa kjhjhk fdasjh kafsdj hkadf 
current database: 'databasename' alot of lorem ipsum data current username: 'username'
and jet more lorem ipsum data and so on

Now i want to get the string after "current database" and the one after "current username". BUT i want to assign them to the strings $database and $username.

I know there is a regular expression like

preg_match_all("/'(\w*)'/", $output, $result);

But in the lorem ipsum-data there is a lot of other text with quotes. And i just want to get only thoose two strings and assign them to there variables.

Does anyone please can help me to figure this out.

2
  • Use current database and current username in your pattern. Commented Jun 11, 2014 at 6:59
  • That pattern which you have is not even remotely going to match your those values Commented Jun 11, 2014 at 7:00

1 Answer 1

1

You can use something like this:

$subject = "lorem ipsum fashjdf hsakj sdhsdaf something: 'data' jhjkhf akjh fa kjhjhk fdasjh kafsdj hkadf 
current database: 'databasename' alot of lorem ipsum data current username: 'username'
and jet more lorem ipsum data and so on";

preg_match("/current database: '([^']+)'/", $subject, $matches);
$database = $matches[1];

preg_match("/current username: '([^']+)'/", $subject, $matches);
$username = $matches[1];

var_dump($database);
var_dump($username);
Sign up to request clarification or add additional context in comments.

1 Comment

It works. Apparently the string had more spaces then it was showing me. So it was "current database: 'databasename'" thats why my regex didn't worked out.

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.