Scott
Scott

Reputation: 5379

PHP Simple String Manipulation

I have a string with multiple lines. For this example, my string will be this:

Name:Jaxo
Description:A person on Stackoverflow
Question:$this->questionName();

How could I get just, say, the 'Description'? I need everything after the description, ie 'A person on Stackoverflow'. I've tried a regex like this, but it doesn't work: /^Description:(.+?)\n/i

Any help is much appreciated!

Thanks

-Jaxo

Upvotes: 0

Views: 67

Answers (3)

The Mask
The Mask

Reputation: 17477

Try this:

$a="Name:Jaxo
Description:A person on Stackoverflow
Question:\$this->questionName();"; 

preg_match("/Description:([^\n]+)/i",$a,$m);
print_r($m);

Output:

Array ( [0] => Description:A person on Stackoverflow [1] => A person on Stackoverflow ) 

Upvotes: 0

uadnal
uadnal

Reputation: 11445

If there is a newline character separating each part of the label you could explode.

$array = explode("\n",$string); // separate params
$desc = explode(":",$array[1]); // separate description

This way you could get any of the parameters.

Upvotes: 0

citizen conn
citizen conn

Reputation: 15390

This should work for you:

if (preg_match('/Description:(.+)/im', $subject, $regs)) {
    $result = $regs[1];
} else {
    $result = "";
}

Where $result is the Description name.

Upvotes: 1

Related Questions