1

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.

flag

75% accept rate

5 Answers

2

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)

link|flag
I wound up using explode , but using my tags (<b> or " in the above example), and then logically figuring out which ones were contained in the tags. Thanks! – Corey Maass Feb 19 at 17:37
2

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

link|flag
1

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]);
link|flag
0
<?php

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

?>

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

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

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.