开发者

get certain string inside string PHP

Let say I have this string:

$myString = "I love to [answer]PROGRAM[/answer]";
开发者_StackOverflow社区

How can I parse this string to get the string "PROGRAM" in PHP?

Thanks,

I really appreciate the help.


You can use the preg_match function to extract the stuff between [answer] and [/answer]

$myString = "I love to [answer]PROGRAM[/answer]";

if(preg_match('!\[answer\](.*?)\[/answer\]!',$myString,$matches)) {
   $answer = $matches[1];
}

See it


use explode and then an array position... example:

$text = "my string [tag]separated[tag] by tags";
$array = explode("[tag]",$text);

echo $array[1]; // "separated"

The explode will split your string into arrays cut by the string "[tag]", you can change the first parameter of the function to everything you want, in this case "[answer]".

Luck ;)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜