kduma
kduma

Reputation: 48

Getting text between PHP code blocks

I want to make RegEXP to get text between php code blocks

for exsample i have this code and i want to get TEXT 1 and TEXT2

 <?php some code ?> TEXT1 <?php some code {?> TEXT2 <?php }some code?>

Upvotes: 2

Views: 212

Answers (3)

netcoder
netcoder

Reputation: 67695

Use the tokenizer instead of regular expressions:

$input = '<?php some code ?> TEXT1 <?php some code {?> TEXT2 <?php }some code?>';

$tokens = token_get_all($input);
foreach ($tokens as $token) {
   if ($token[0] == T_INLINE_HTML) {
       echo $token[1];
   }
}

Output:

 TEXT1  TEXT2 

Upvotes: 4

Nenad
Nenad

Reputation: 3556

Mybe something like

(?<=^|>)[^><]+?(?=<|$)

Upvotes: 0

rpaskett
rpaskett

Reputation: 456

It is possible, as you can use file_get_contents() on the php file and it will return the raw contents without parsing the PHP. As for the regex, I'd recommend the regex cheatsheet by dave child at addedbytes.com. http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/ That should help you piece together a regex for your problem.

Upvotes: 0

Related Questions