2

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?>
2
  • Where is the text in TEXT1 and TEXT2 coming from? Commented Jun 17, 2011 at 17:16
  • @netcoder I forgot to write that text1 may be html Commented Jun 17, 2011 at 17:32

3 Answers 3

4

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 
Sign up to request clarification or add additional context in comments.

2 Comments

I forgot to write that text1 may be html
@Ortal: If it's HTML and you just want the text (without the tags), just use strip_tags on the value afterwards. :)
0

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.

Comments

0

Mybe something like

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

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.