开发者

preg_grep on a larger string

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 bette开发者_如何学编程r way to do preg_grep on on a large string?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜