开发者

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: /^D开发者_运维知识库escription:(.+?)\n/i

Any help is much appreciated!

Thanks

-Jaxo


This should work for you:

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

Where $result is the Description name.


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.


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 ) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜