views:

56

answers:

1

Not sure what this is called, but is there a way I can pull data from a file, echo it, but ignore data within a certain set of characters?

<?php
echo file_get_contents('test.txt');
?>

alt text

Would it be possible to ignore the data in between the asterisks, and the asterisks themselves when I echo out the final function?

alt text

+1  A: 

You wouldn't "ignore it" as much as you would "replace it with nothing"

A quick 'n' dirty approach

echo preg_replace( "/\*{3}.*?\*{3}/", "", file_get_contents( 'test.txt' ) );
Peter Bailey
Worked flawlessly. Now with that, I can experiment with other strings.
Homework
Note that this regex would be "greedy", so "this *** some text *** is a *** other text *** test" would come out as "this test", rather than "this is a test", as you may expect.
Lucas Oman
No it's not. I specifically and deliberately made the subpattern ungreedy. And I believe the test string you intended to indicated is `"this *** some text *** is a *** other text *** test"`
Peter Bailey
You're right, I just noticed the '?'. And stupid markup.
Lucas Oman