Question

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 basically to get {{testto}} in a array, every occurrence 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?

Was it helpful?

Solution

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];

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top