0

Using the sample code below, I want to extract all code between each <<BEGIN>> and <<END>> tag and append the extracted code to an array for further processing later on.

<?php
$html = '<<BEGIN>><div>Some text goes here...</div><<END>><<BEGIN>><table border="0"><tr><td>Table cell text goes here</td></tr></table><<END>><<BEGIN>><ul><li>My string</li><li>Another string</li></ul><<END>>';
?>

The end result needs to look like this:

Array (
    [0] => '<div>Some text goes here...</div>'
    [1] => '<table border="0"><tr><td>Table cell text goes here</td></tr></table>'
    [2] => '<ul><li>My string</li><li>Another string</li></ul>'
)

Hope this makes sense.

Any assistance would be greatly appreciated. Thanks in advance.

4
  • look into preg_match_all. you have some pretty basic rules, so the regex should be pretty easy to figure out. php.net/manual/en/function.preg-match-all.php Commented Feb 13, 2015 at 16:50
  • Wouldn't using a database be easier? Commented Feb 13, 2015 at 16:50
  • I used this for all of my stuff stackoverflow.com/questions/5696412/… Commented Feb 13, 2015 at 16:53
  • I would say you'll need to use a parser, as with a regex having only parent tags between each <<begin>> will require some heavy complex regex simplehtmldom.sourceforge.net Commented Feb 13, 2015 at 16:54

2 Answers 2

3

Use pretg_match_all() function to do this.

<?php

    $html = '<<BEGIN>><div>Some text goes here...</div><<END>><<BEGIN>><table border="0"><tr><td>Table cell text goes here</td></tr></table><<END>><<BEGIN>><ul><li>My string</li><li>Another string</li></ul><<END>>';

    preg_match_all("/<<BEGIN>>(.*)<<END>>/", $html, $result);

    echo '<pre>';
    print_r($result[1]);
    echo '</pre>';

?>

Show the source code of the page and you will see all you wanted :) (, ...)

Sign up to request clarification or add additional context in comments.

2 Comments

I did the full regex, and print_r the result he wanted. Don't forget to upvote if all is clear :)
You can check my answer in green to write that's resolute :) You're welcome !
1

You can strip the <<END>> and <<BEGIN>> from start and finish of the string and then explode on <<END>><<BEGIN>> .

$html = substr($html, 9);
$htmlleng = strlen($html) - 7;
$html = substr($html, 0, $htmlleng);
$myarray = explode('<<END>><<BEGIN>>', $html)

(You can make that more elegant but it shows what you would need to accomplish.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.