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

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

问题描述:

I have a chunk of text like:

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

OR

"First content" and then "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.

我有一大块文字,如: p>

 &lt;  b&gt;第一内容&lt; / b&gt; 然后是&lt; b&gt;第二内容&lt; / b&gt; 
  code>  pre> 
 
 

OR p>

 “First content”然后 “第二个内容”
  code>  pre> 
 
 

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

从任一示例中,我获取两个标记之间的内容。 我已经弄清楚如何获得第一个实例,但无法弄清楚使它递归的最佳方法。 p> div>

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.

<?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.

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

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]);