BSlepkov Posted September 24, 2008 Share Posted September 24, 2008 First timer here. Greetings. Ok. First off, I've gotten damn good at this php stuff, however this one's got me stumped. Hope there's an answer out there. Here's what I'm trying to do: I've written a script that searches my website files (php, txt and doc) for given phrases. Once I've determined a relevance factor, I want to display (a portion of) a paragraph containing the found phrase. For now, let's focus on an example php file. I'm working with: $rawtext=file_get_contents($file, 'FILE_TEXT'); The following line gives me a paragraph. The (adjusted) results for each variation follow. BTW, the search phrase is smart growth: preg_match('/(^|\n)(.*?'.$found[0].'.*?)(\n|$)/i', $rawtext, $found); $excerpt=htmlentities($found[0]); Gives me: <p />My last ten years have been dedicated to <a href="<?=$DbaseSite?>information-and-stats.php" target="_blank">researching matters pertaining to local governance, urban renewal and 'sustainability'</a>. For the past four of those years <a href="<?=$RootDir?>about.php" target="_blank">I have involved myself in related policy-defining initiatives</a> at all levels of government. All of those initiatives relate to smart growth,<?=$WStart?>Smart%20Growth<?=$WStop .$GStart?>Smart%20Growth<?=$GStop .$SStart?>Smart%20Growth<?=$SStop?> urban renewal,<?=$WStart?>Urban%20Renewal<?=$WStop .$SStart?>Urban%20Renewal<?=$SStop .$GStart?>Urban%20Renewal<?=$GStop?> and for the most part, socio-economic sustainability. I am making my position known, as I have a vested interest in how my city and region address those matters that are defining 21<sup>st</sup> Century society. $excerpt=$found[0]; ... gives me: My last ten years have been dedicated to researching matters pertaining to local governance, urban renewal and 'sustainability'. For the past four of those years I have involved myself in related policy-defining initiatives at all levels of government. All of those initiatives relate to smart growth,Smart%20GrowthSmart%20GrowthSmart%20Growth urban renewal,Urban%20RenewalUrban%20RenewalUrban%20Renewal and for the most part, socio-economic sustainability. I am making my position known, as I have a vested interest in how my city and region address those matters that are defining 21st Century society. The problem of course, stems from snipets like '<?=$WStart?>' not being processed. Before attempting to just use strip_tags($excerpt), I've tried to get around this issue by first replacing strings like '<?=$WStart?>Urban%20Renewal<?=$WStop .$SStart?>Urban%20Renewal<?=$SStop .$GStart?>Urban%20Renewal<?=$GStop?>' with: $excerpt=preg_replace('\<\?=\?\>([a-z]+)(\+|%20|\-)?([a-z]+)?\<\?= ?\.\?\>([a-z]+)(\+|%20|\-)?([a-z]+)?\<\?= ?\.\?\>([a-z]+)(\+|%20|\-)?([a-z]+)?\<\?=\?\>', '', $excerpt); I'm sure there's a better regex formula, but that's not the point. I've testing this out ten ways to Sunday, and it appears that for some reason, '<' and/or '>' result in blanking out the entire variable. As a test, ... preg_replace('\?=\?', 'oops', $excerpt) ... worked, but even preg_replace('\>([a-z]+)(\+|%20|\-)?([a-z]+)?\<', '><', $excerpt) yields an empty $excerpt. So then...what's happening, or not? As best I can tell, there's no reason for the variables in the short tags not to be processed, but now that I think of it, I should try to replace <?=$variable?> with <?php echo $variable ?>. Seems I also managed, without luck, replacing <?php echo $variable ?> with {$variable}. Finally, I'm not seeing any errors being registered during any of these efforts. Thanks for your time. B. Link to comment https://forums.phpfreaks.com/topic/125692-solved-file_get_contents-not-processing-variable/ Share on other sites More sharing options...
rarebit Posted September 24, 2008 Share Posted September 24, 2008 If you provided the variables filled in, within a function and then did something like this: ob_start(); $x = eval("?>".$s); $l = ob_get_contents(); ob_end_clean(); Then that would parse it as if for real man! p.s. the ob_start and ob_end_clean stop it from dumping straight to stdout... Link to comment https://forums.phpfreaks.com/topic/125692-solved-file_get_contents-not-processing-variable/#findComment-649944 Share on other sites More sharing options...
BSlepkov Posted September 25, 2008 Author Share Posted September 25, 2008 Thanks for that. I'll play around with it and see what happens. Using ob's is going to be a new territory for me. So I've got a few questions... 1. Can I use ob_start at any point in a script, i.e. after headers have been called and contents partially displayed? 2. When you say, if I provided the variables filled in, to which (set of) variables are you referring? $rawtext, <?=WStart?>, $excerpt? 3. I'm looking at your solution, trying to think how and where I should apply it. Is $x or $s supposed to be any one a the variables I mentioned? Link to comment https://forums.phpfreaks.com/topic/125692-solved-file_get_contents-not-processing-variable/#findComment-650324 Share on other sites More sharing options...
BSlepkov Posted September 25, 2008 Author Share Posted September 25, 2008 Actually rarebit, the more I look at your solution and research into ob's usage, the more confused I become as to how and where to apply it in my case. I, and less experienced php'ers could use a more expounded answer. Otherwise, for me, I'll be spending the next few days playing around. Also, bear in mind that in my particular circumstances I think that the best approach is completely replace/remove passages such as <?=$WStart?>Urban%20Renewal<?=$WStop .$SStart?>Urban%20Renewal<?=$SStop .$GStart?>Urban%20Renewal<?=$GStop?> from $excerpt followed by $excerpt=strip_tags($excerpt) Otherwise, in order to maintain the integrity of any site related links appearing in the excerpted html I would have to do more programming than I care to at this point. Link to comment https://forums.phpfreaks.com/topic/125692-solved-file_get_contents-not-processing-variable/#findComment-650413 Share on other sites More sharing options...
rarebit Posted September 25, 2008 Share Posted September 25, 2008 Thats how I learnt, and I also think it's the best way... But if you just want the easy answer, then: <?php $WStart = "xxx"; $WStop = "yyy"; $SStart = "zzz"; $SStop = "aaa"; $GStart = "bbb"; $GStop = "ccc"; $s = "<?=\$WStart?>Urban%20Renewal<?=\$WStop .\$SStart?>Urban%20Renewal<?=\$SStop .\$GStart?>Urban%20Renewal<?=\$GStop?>"; ob_start(); $x = eval("?>".$s); $l = ob_get_contents(); ob_end_clean(); print $l."<br>"; ?> I've added escaping slashes to the original string otherwise in this situation the variables would replace them. If reading a file or something, this wouldn't be a problem. After you've called 'ob_start()' nothing will be outputted to stdout until you call some form of 'ob_end'... Not that i've recently read the manual, but i'd say $x holds the result, e.g. if it executed ok... Link to comment https://forums.phpfreaks.com/topic/125692-solved-file_get_contents-not-processing-variable/#findComment-650420 Share on other sites More sharing options...
BSlepkov Posted September 25, 2008 Author Share Posted September 25, 2008 Thanks, rarebit. Your answer still leaves me with much to struggle with, but it's a start. And yes, I too learned by just struggling with trial and error - an ongoing process that never really ends. Link to comment https://forums.phpfreaks.com/topic/125692-solved-file_get_contents-not-processing-variable/#findComment-650529 Share on other sites More sharing options...
BSlepkov Posted September 26, 2008 Author Share Posted September 26, 2008 I don't know what to tell you, rarebit. Heaven knows I tried it your way without any success. That's not to say it wouldn't work provided all the correct combinations of php functions were applied in the proper sequence. However, your last post did help remind me that since I was working within a user function, I first needed to extract previously defined variables. The following is what ultimately worked for me. I hope others manage to stumble on this in their search for similar solutions. With the original $excerpt in hand, I called PhpCleanup() ... below function PhpCleanup() { global $excerpt; $fixedstr=$excerpt; # NOTE! For whatever the reason, the regex search pattern in preg functions must be enclosed thusly ... '//' or "//" $varextract=preg_match_all('/\$([a-zA-Z0-9]*)/', $excerpt, $vars); $varlisting=$vars[0]; foreach ($varlisting as $key=>$var) { if (!$$var) { $varname=$vars[1][$key]; $$var=$GLOBALS[$varname]; $fixedstr=preg_replace('/(<\?=)?\$' .$varname .'( ?\.|\?>)/', $$var, $fixedstr); } } $fixedstr=htmlspecialchars_decode($fixedstr); return $fixedstr; } // end PhpCleanup Other than the note I embedded in the function above, here's what I learned in my struggles: 1. Since I originally converted $fixedstr with $fixedstr=htmlentities($excerpt) in order to get the preg_replace to acknowledge the '<' and '>' characters, the preg_replace had to be constructed thusly ... preg_replace('/(<\?=)?\$' .$varname .'( ?\.|\?>)/', $$var, $fixedstr); 2. I needed $fixedstr=htmlspecialchars_decode($fixedstr) in order to get the final results displayed as true HTML Link to comment https://forums.phpfreaks.com/topic/125692-solved-file_get_contents-not-processing-variable/#findComment-651268 Share on other sites More sharing options...
BSlepkov Posted September 26, 2008 Author Share Posted September 26, 2008 Just a minor correction to my last post. Since I didn't call PhpCleanup() with $excerpt=PhpCleanup(), the final lines in that function should be ... $excerpt=htmlspecialchars_decode($fixedstr) ... not $fixedstr= and the return statement is not needed. FTI - establishing $fixedstr in the first place isn't needed, except I did so to monitor my progress on replacing each variation of the variables. Link to comment https://forums.phpfreaks.com/topic/125692-solved-file_get_contents-not-processing-variable/#findComment-651279 Share on other sites More sharing options...
BSlepkov Posted September 26, 2008 Author Share Posted September 26, 2008 Well, well, well. Rarebit, I'm pleased to say that your suggestion ultimately did pay off. There are instances, where, after running through my PhpCleanup function, $excerpt did indeed contain strings like in this example: ... I have <?php hotlink('letters of recommendation', true, "http://mywebsite/references.php"); footnote(); ?> from the ... In order to resolve such cases, then after this ... $excerpt=PhpCleanup($found[0]); then ... ob_start(); $excerpt = eval("?>".$excerpt); $excerpt = ob_get_contents(); ob_end_clean(); ... forced the <?php script to be assessed. So, an extra thank you goes out to rarebit! Link to comment https://forums.phpfreaks.com/topic/125692-solved-file_get_contents-not-processing-variable/#findComment-651563 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.