0

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

1
  • 'description' not is in start of string,you need remove ^ for your regex works fine. Commented Jul 8, 2011 at 21:00

3 Answers 3

1

This should work for you:

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

Where $result is the Description name.

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

1 Comment

Worked perfectly, thanks!! I shortened it to this, as well: echo $result=preg_match('/Description:(.+)/im',$subject,$r)?$r[1]:null;
0

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.

1 Comment

Correct, but if there are semicolons in the description then count($desc) > 2 (just a specification).
0

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 ) 

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.