phppup Posted November 3, 2021 Share Posted November 3, 2021 I'm trying to gain access to a file for the purpose of extracting a portion of it for use with JavaScript. If there's an advisable methodology that can be shared, that would be GREAT. The best that I've come up with is to use readfile("sample.php"); to gain access to the data. However, I then want to run a JavaScript on this data. Is there a recommended way to accomplish this? The only way I can think of is to move the data into a variable, but my effort has a bug. echo readfile("sample.php"); //provides a viable result //but then... $my_variable = readfile("sample.php"); echo $my_variable; //provides a number (I have not determined what that number represents... perhaps a line or character count??) //Note: print_r($my_variable) provides same result How can I achieve a successful result and transfer the data so that JavaScript can affect it? Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/ Share on other sites More sharing options...
Barand Posted November 3, 2021 Share Posted November 3, 2021 (edited) 46 minutes ago, phppup said: I have not determined what that number represents Perhaps if you referenced the manual occasionally you would know exactly what it returns and why it's the wrong function. readfile() file_get_contents() file() Edited November 3, 2021 by Barand 1 Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591686 Share on other sites More sharing options...
phppup Posted November 3, 2021 Author Share Posted November 3, 2021 @Barand I have been read the manual. And, file_get_contents made everything go bonkers. I came here in a moment of frustration (knowing I was missed). But if I am on the right path, then I will continue my journey. Thank you, sensei. Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591694 Share on other sites More sharing options...
ginerjm Posted November 3, 2021 Share Posted November 3, 2021 If you want to examine this file then you need to read it in and go thru it line/char by line/char. The function you are using (as it says in the manual) is not going to do that for you. Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591698 Share on other sites More sharing options...
phppup Posted November 3, 2021 Author Share Posted November 3, 2021 @ginerjm And what (in PHP) will? This is why I felt that moving it into JavaScript thru a variable was a fair solution. Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591703 Share on other sites More sharing options...
ginerjm Posted November 3, 2021 Share Posted November 3, 2021 (edited) So you are not a PHP coder? My example: $fname = 'BeerBottleBand.txt'; if (is_file($root.$fname)) { $hdl = fopen($root . $fname,'r'); while($line = fread($hdl,1000)) echo "$line<br>"; fclose($hdl); } else echo "File '$root$fname' not found"; Have fun! Edited November 3, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591704 Share on other sites More sharing options...
Barand Posted November 3, 2021 Share Posted November 3, 2021 As you have told us nothing about the file or the portion you want from it, how do you expect us to help you? Why is your data stored in a php file, especially as you apparently need to use it elsewhere? Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591706 Share on other sites More sharing options...
phppup Posted November 3, 2021 Author Share Posted November 3, 2021 @ginerjm I am not a code by either profession or training. More like an overzealous hobbyist. And while I understand your example, it's not something that I would have thought of myself, nor have I seen examples like it in my search engine queries. So, thanks. Now the question is, how (if possible) would I avoid JavaScript and have PHP pull results from those resulting lines in your example? My current thought process is to use document.querySelector in order to loop through and pull data to use on a new script. @Barand Admittedly, this is something that came up AFTER implementation of other ideas, so it may be a backwards approach. But for now, it's (hopefully) easier than re-structuring my other files. So, getting info that already exists and re-using it in this manner just might work. Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591708 Share on other sites More sharing options...
ginerjm Posted November 3, 2021 Share Posted November 3, 2021 I have showed you how to read the data and output it only. If you want to add some code to select the pieces of data that you want to find, that's for you to do yet. There are LOTS of string functions to use to examine the data to find what you are looking for. Take a look at the manual. Look up 'string' in the search button Here Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591710 Share on other sites More sharing options...
ginerjm Posted November 3, 2021 Share Posted November 3, 2021 (edited) Some thoughts. Do you know the layout of your data file? Is it just a big 'paragraph' of text or is it laid out in lines that you can search one at a time for what you are looking for? I have given you the 'read line by line' approach but you might want to use the "file_get_contents()" function that was pointed out to you earlier. That will load the entire data file into a string variable on which you could then use a stripos() function call to locate the specific string that you are seeking and the the substr() function to extract that part to a separate variable. All of which can be found in the manual via the link I gave you previously. Sound like a plan? Edited November 3, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591713 Share on other sites More sharing options...
phppup Posted November 3, 2021 Author Share Posted November 3, 2021 Some good information that I will have to try out. Since I am already somewhat invested in the JavaScript method, it will be interesting to put them side by side to see if I get identical results. Since I need to"read" the file from the server, it would be much cleaner to keep this completely within a PHP format. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591717 Share on other sites More sharing options...
ginerjm Posted November 3, 2021 Share Posted November 3, 2021 I personally see no reason at all to use JS on this task. Your data is on a server, not on a client. So use PHP to examine it and to build the response and view the results using php and html. Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591719 Share on other sites More sharing options...
phppup Posted November 3, 2021 Author Share Posted November 3, 2021 (edited) @ginerjm I think I became intrigued with querySelectorAll. Having said that, what is the best way to move through the data (most likely as a string) to achieve an accumulation of like instances. eg: find all instances of "cat" Do I count the line that the first instance occurred on and create a clause to search for an instance where $line > $the_previous ? Edited November 3, 2021 by phppup Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591721 Share on other sites More sharing options...
ginerjm Posted November 3, 2021 Share Posted November 3, 2021 (edited) Have you looked into the manual's many 'string' functions yet? Time to do your own research if you want to become more than a mere hobbyist. Try this: https://www.php.net/manual/en/ref.strings.php Do you want to find exactly 'cat'? If so, you should look for " cat " to avoid getting 'scat' or 'catcher'. And why do you want all the occurrences of a simple word? Edited November 3, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591722 Share on other sites More sharing options...
ginerjm Posted November 3, 2021 Share Posted November 3, 2021 Here. $fname = 'BeerBottleBand.txt'; $sch_str = 'th'; echo "Looking for file $root$fname<br>"; if (is_file("$root$fname")) { echo "Found file $root$fname<br><br>"; $contents = file_get_contents("$root$fname"); echo "Contents are:<br><br>$contents<br><br>"; $cnt = substr_count(strtoupper($contents), strtoupper($sch_str)); echo "Found $cnt occurrences of '$sch_str' in all cases.<br>"; } else echo "File '$root$fname' not found"; If you want an exact match (ie, not all cases) then remove the strtoupper calls. Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591723 Share on other sites More sharing options...
phppup Posted November 3, 2021 Author Share Posted November 3, 2021 Thanks. It's a great starting point. And yes, I'm very familiar with the manuals string reference pages (and have found them helpful in the past). Let's see where all this takes me next. LOL Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591724 Share on other sites More sharing options...
phppup Posted November 3, 2021 Author Share Posted November 3, 2021 I see how substr_count works. But I want to add to it with echo "Occurrence $cnt says" . $blahblahblah Is there a built-in function to use for this? Or do I need to create a loop like if( i=0; i<$cnt; i++){ //if this position was already used then continue //otherwise, i assume i will get there same data i times } to get a complete listing? Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591725 Share on other sites More sharing options...
ginerjm Posted November 3, 2021 Share Posted November 3, 2021 (edited) So if you are searching for 'cat' you want to see the word 'cat' every time you hit it? What's the point of that? How about telling us/me what this exercise is supposed to accomplish? In real-world speak, not coding speak. And what do you mean by 'this position'? This is not a byte by byte search - it is a find a string search. Edited November 3, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591726 Share on other sites More sharing options...
ginerjm Posted November 3, 2021 Share Posted November 3, 2021 Do you want to find the chars before and after every occurrence of your search argument? Is that why the position comes up here? If so, then the code I gave you is not what you need. But I would really like to know what your task is. Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591727 Share on other sites More sharing options...
phppup Posted November 3, 2021 Author Share Posted November 3, 2021 If "cat" is found, then I want the entire sentence that contains cat. I imagine I can find a way to get from "cat" to a period (at end of sentence). Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591728 Share on other sites More sharing options...
ginerjm Posted November 3, 2021 Share Posted November 3, 2021 Aha! Let me work on that. Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591729 Share on other sites More sharing options...
phppup Posted November 3, 2021 Author Share Posted November 3, 2021 This is why I inquired about the loop. Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591730 Share on other sites More sharing options...
Barand Posted November 3, 2021 Share Posted November 3, 2021 (edited) No loop required $text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus cat lectus malesuada libero, sit amet commodo magna eros quis urna. "; $srch = 'cat'; $p1 = strpos($text, $srch, 0); // find position of 'cat' if ($p1===false) die("'$srch' not found"); echo "$srch found at pos $p1<br>"; $p2 = strpos($text, '.', $p1); // find position of '.' following 'cat' echo substr($text, $p1, $p2-$p1+1); // get text beteen those points. Edited November 3, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591731 Share on other sites More sharing options...
phppup Posted November 3, 2021 Author Share Posted November 3, 2021 @Barand Thanks. But this solution looks like it will only identify one (the first) instance of cat. Suppose the file contained assorted sentences of "Funny things my pet does." While each paragraph will contain a sentence "My [type of pet] does this silly thing [period]." not every paragraph will contain "cat" because a "cat" is not the only pet. Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591732 Share on other sites More sharing options...
Barand Posted November 3, 2021 Share Posted November 3, 2021 That was not stated in your question. Deal with it. I've given you the basics - work on it. If you know which pets are in there, you stand a chance. If you don't, you are going to need another way to identify the text portions you want. Quote Link to comment https://forums.phpfreaks.com/topic/314158-using-a-readfile-value-as-a-variable/#findComment-1591734 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.