Jump to content

using a readfile() value as a variable


phppup

Recommended Posts

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?

Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

@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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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 by phppup
Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

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 by Barand
Link to comment
Share on other sites

@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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.