开发者

in PHP, how do I create an array of all strings between two strings?

I have a chunk of text like:

<b>First content</b> followed by <b>second content</b>

OR

"First content" and then 开发者_如何学JAVA"second content"

And I want to create an array that looks: [1] => "First content" [2] => "second content"

From either example, where I get the content between two tags. I've figured out how to get the first instances, but can't figure out the best way to make it recursive.


If you know what exactly is between "First content" and "second content", use explode:

http://php.net/manual/en/function.explode.php

e.g.

$values = explode("followed by", $yourtext);

(use "strip_tags" on your array-elements if you just want the plaintext)


If you are looking to extract all content between specific tags, you may be best off with a HTML DOM Parser like simplehtmldom.


basically you need a regexp in form "START(.+?)END", for example

$a = "<b>First content</b> followed by <b>second content</b>";
preg_match_all("~<b>(.+?)</b>~", $a, $m);
print_r($m[1]);


$string="<b>First content</b> followed by <b>second content</b>";
$s = explode("followed by", strip_tags($string));
print_r($s);


<?php

$yourArray = split(" followed by ", $yourString);

?>

As long as you have the same text in between each value that you want to break up into an array you can use this example.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜