tags:

views:

132

answers:

2

Hi!

I need to use preg_reg on some string with content from a html file.

    $file = file_get_contents($path);
    $html_array = explode(' ', $file);

The problem is that the array looks sometimes like this:

[77]=>
  string(35) "<div>
</div>
<br>
{{testto}}
<br>"

I have tried to put in some whitespaces there.. :P Won't work.. :/ Later on I will do a preg_grep like this:

    $childframes = preg_grep('!\{\{(\w+)\}\}!', $html_array);
    $retur = array();
    foreach($childframes as $v){
        $v = trim($v);
        $retur[] = substr($v, 2, -2);
    }

    return $retur;

So the idea is basicly to get {{testto}} in a array, every ocurrence of {{sometext}} where I substring it down to only 'sometext'.

Thanks =)

EDIT: To repeat the problem: explode is not working right so I need some regex if possible, instead of just whitespace.., and is there any better way to do preg_grep on on a large string?

A: 

Don't you just want to use:

strip_tags($v)

first so that you are removing all of the html tags, then your code will get the 'sometext' correctly.

Arthur Frankel
never seen that function, thanks :)
Johannes
+1  A: 

No need to explode, create a temporary array, grep it and strip it later, just a regexp match:

$html = file_get_contents($path);
preg_match_all('#{{(.*?)}}#', $html, $matches);
$array_of_texts = $matches[1];
kemp
wow! nice :D that was so much better, thanks =)
Johannes