Is it possible to replace part of text or HTML file with PHP? I'm loading portion of file into text editor by using preg_match to extract text only between certain tags. Now when finish editing I want to update the same file with changes made, and replace the same part previously loaded.
since you're using preg_match, you can use the preg_replace after editing and store it to the file.
for example if you are loading a UI for that then you might be doing this on file1.php
$data = file_get_contents($filename);
//do regex here
$values = preg_match($pattern,$data);
//do necessary display here for the form I assume
then on file2.php that receives the request on the form, you do exacly the same thing
$data = file_get_contents($filename);
//compose the string to you will have to replace to the pattern
$data = preg_replace($pattern,$replace,$data);
//then write to the same file
file_put_contents($filename,$data);
these are just theoretical, kindly check with the php manual for correct syntax or parameters
Related
I need to check if a webpage outside of my site has a specific word on it. I’ve tried file_get_contents() but it doesn’t return anything. Is there any way I can do this in PHP?
edit: Here’s what I’ve tried:
$query = 'example';
$file = "https:// www.site.com/search?q=$query";
// tested url and it works, had to add space to post it
$contents = file_get_contents($file);
echo $contents;
I was expecting it to just output the entire page for me to use .includes() on later but it just doesn’t output anything.
Look into curl to get the contents of a web page. Then you can use preg_match to find the word.
I am trying to hit a URL after generating the data to be filled for the parameters that are passed in URL using Python in back end. So the flow is:
User lands on a page with a form having some drop downs.
Python code in the backend reads the content from a file and returns single output based on some conditions for each of the dropdown.
User hits the submit button with the data.
The data gets generated correctly but when I hit submit button, I get %0D%0A characters at the end of the parameter values in the URL
E.g., sample.php?param1=20%0D%0A¶m2=50%0D%0A
How do I get rid of these values as this is causing trouble with the other code where I am using these values?
I take it you read the data from a file, so probably reading the file causes the line endings to be read as well.
In any case, try using strip() or rstrip() in your Python code to remove all/trailing whitespace before your assemble the target URL.
I understand that it's actually a PHP script that assembles the URL. In that case, use PHP's trim() function on the variables you use to assemble the URL.
For example: Assume that $val1 and $val2 are read from a file or some other place. Then the following line assembles above URL stripping whitespace from $val1 and $val2.
$url = "sample.php?param1=" . trim($val1) . "¶m2=" . trim($val2);
Some browsers do that automatically, you can try decoding it back using urldecode()
http://php.net/manual/en/function.urldecode.php
Try this :
<?Php
$str = "Your Inputed Value or string"
$url = str_replace(" ","-", $str);
?>
Link Menu
ive got the following fwrite code, with , separating the data and it ending in ))
$shapeType = $_POST['shapeType'].','.$_POST['triangleSide1'].','.$_POST['triangleSide2']
.','.$_POST['triangleSide3'].','.$_POST['triangleColour'].'))';
fwrite($handle, $shapeType);
but this is how it saves in the text file...
,,,,))Triangle,180,120,80,Red))
why have the first set of
,,,,,))
appeared in front of what it should look like?
You need to add a new line character to the end of each line. Otherwise your lines will all run into each other.
Use PHP_EOL for this as it will automatically use the Operating System appropriate new line character sequence.
PHP_EOL (string)
The correct 'End Of Line' symbol for this platform.
Available since PHP 4.3.10 and PHP 5.0.2
$shapeType = $_POST['shapeType'].','.$_POST['triangleSide1'].','.$_POST['triangleSide2']
.','.$_POST['triangleSide3'].','.$_POST['triangleColour'].'))'.PHP_EOL;
FYI, this might be a little cleaner to do using sprintf():
$shapeType = sprintf("%s,%s,%s,%s,%s))%s",
$_POST['shapeType'],
$_POST['triangleSide1'],
$_POST['triangleSide2'],
$_POST['triangleSide3'],
$_POST['triangleColour'],
PHP_EOL
);
Without seeing more of the code I would guess that you post to the same file and you do not check if a POST request was made before you write your file. So probably you write to your file on a GET request as well, causing empty entries to appear.
You would need something like:
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
// ...
$shapeType = $_POST['shapeType'].','.$_POST['triangleSide1'].','.$_POST['triangleSide2']
.','.$_POST['triangleSide3'].','.$_POST['triangleColour'].'))';
fwrite($handle, $shapeType);
// ...
}
Edit: By the way, you should probably use fputcsv as that takes care of escaping quotes, should you change something in the future that adds for example a description field.
Very simply, i want to make a variable reads the html code as string ,, i mean dont execute it (run it) .
the problem with the code is : i have a html file , and i want to get the content of it , and make some preg_replace for it (run a function on the html code), the problem is i cant use preg_replace, or any another function because the html code is executed by php (php reads the html code)..
i wish you understand me, i want something like highlight_string, but it save the html code in the variable.
Thank you.
you're probably trying to include or require the HTML code.
which is incorrect since it is evaluated as part of the source.
instead, use a function such as file_get_contents() to read the file into a string.
Use file_get_contents() as #David Chan suggested and then pass the result through htmlentities()... it converts the characters to HTML entities (i.e., < to <).
$getTheContent = file_get_contents($filepath);
echo htmlentities($getTheContent);
It should return the code, not executed.
I have this code:
$newphrase = str_replace('href="/Css/IE6.css"', 'href="http://www.company.com/pgrddedirect/iefix.css"', 'href="/Css/IE6.css"');
So that I can search the html file in php using DOM in an attempt to modify the location of the .css file before I redisplay it. I intend on uploading the new .css file to my server and when I display the page with my php I want to first edit the location lines of the css so that I can resdisplay it with my own. The code can find and edit the html but I don't know how to save it before displaying it.
Cheers
Why can't you just change the actual link manually (wherever it's defined)?
This is a lot of overhead, especially if your page is big.
This is how str_replace works:
mixed str_replace ( mixed $search , mixed $replace , mixed $subject
So do this:
$newdata = str_replace('href="/Css/IE6.css"', 'href="http://www.company.com/pgrddedirect/iefix.css"', $data);
Where $data is a string containing the original HTML.