在PHP中,如何在两个字符串之间创建所有字符串的数组?

时间:2010-02-19 15:53:58

标签: php parsing string arrays

我有一大堆文字,如:

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

OR

"First content" and then "second content"

我想创建一个看起来像的数组: [1] =&gt; “第一内容” [2] =&gt; “第二内容”

从任何一个例子中,我得到两个标签之间的内容。我已经弄清楚如何获得第一个实例,但无法找出使它递归的最佳方法。

5 个答案:

答案 0 :(得分:2)

如果您希望在特定标记之间提取所有内容,最好使用像simplehtmldom这样的HTML DOM解析器。

答案 1 :(得分:2)

如果你知道“第一内容”和“第二内容”之间究竟是什么,请使用explode

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

e.g。

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

(如果你只想要明文,请在你的数组元素上使用“strip_tags”)

答案 2 :(得分:2)

基本上你需要一个“START(。+?)END”形式的正则表达式,例如

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

答案 3 :(得分:0)

<?php

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

?>

只要在要分解为数组的每个值之间使用相同的文本,就可以使用此示例。

答案 4 :(得分:0)

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