I have written a PHP script that goes through and changes the HTML generated by Visual Page. (I know - it is a REALLY old program - but I like it.) Anyway, in each of these HTML web pages I'm working with I put in:
{copyright}
Where I want the copyright to show up. I did the following loop:
foreach( $file as $k1=>$v1 ){
if( preg_match("/\{copyright\}/i", $v1) ){
$file[$k1] = preg_replace( "/\{copyright\}/i", $copyright, $v1 );
}
}
This DID NOT WORK. I would echo out the $file[$k1] before and after the IF statement so I could see what was going on and PHP just wouldn't change the {copyright} to the copyright. The variable $copyright had something similar to:
<tr><td>Copyright 2007-NOW MyCompany. All rights reserved.</td></tr>
Now - here is the freaky thing: I put a BREAK after it did the preg_replace - and - it worked. So just changing the above to
foreach( $file as $k1=>$v1 ){
if( preg_match("/\{copyright\}/i", $v1) ){
$file[$k1] = preg_replace( "/\{copyright\}/i", $copyright, $v1 );
break;
}
}
Made it work. Does anyone have ANY kind of an idea why? I'm completely stumped by this.
Note: I thought I'd post what I had gotten the HTML down to.
<html>
<head>
<title>Test</title>
</head>
<body>
<table border='1' cellspacing='0' cellpadding='0'><tbody>
<tr><td>This is a test</td></tr>
{copyright}
</tbody></table>
</body>
</html>
That is what I boiled my test case down to.
Also note : I did get this to work. Don't know why I had to put in a BREAK statement and I put it in on a whim. My thinking went "Maybe there is something that is making it re-evaluate the string after the change? Let me try putting in a break statement." I did and - it worked. But I have no idea WHY it worked.
Maybe I'm missing something here but it doesn't seem like you need any regex's here. The str_replace function, http://php.net/str_replace, should work fine for this.
Example:
$string = "<html>
<head>
<title>Test</title>
</head>
<body>
<table border='1' cellspacing='0' cellpadding='0'><tbody>
<tr><td>This is a test</td></tr>
{copyright}
</tbody></table>
</body>
</html>";
$copyright = '<tr><td>Copyright 2007-NOW MyCompany. All rights reserved.</td></tr>';
echo str_replace('{copyright}', $copyright, $string);
Output:
<html>
<head>
<title>Test</title>
</head>
<body>
<table border='1' cellspacing='0' cellpadding='0'><tbody>
<tr><td>This is a test</td></tr>
<tr><td>Copyright 2007-NOW MyCompany. All rights reserved.</td></tr>
</tbody></table>
</body>
</html>
Demo: http://sandbox.onlinephpfunctions.com/code/f53b1a96f270e52392303d7dfb7c327372747d0b
Update per comment:
$string = "<html>
<head>
<title>Test</title>
</head>
<body>
<table border='1' cellspacing='0' cellpadding='0'><tbody>
<tr><td>This is a test</td></tr>
{copyright}
</tbody></table>
</body>
</html>";
$copyright = '<tr><td>Copyright 2007-NOW MyCompany. All rights reserved.</td></tr>';
foreach(explode("\n", $string) as $line) {
echo str_replace('{copyright}', $copyright, $line) . "\n";
}
Output:
<html>
<head>
<title>Test</title>
</head>
<body>
<table border='1' cellspacing='0' cellpadding='0'><tbody>
<tr><td>This is a test</td></tr>
<tr><td>Copyright 2007-NOW MyCompany. All rights reserved.</td></tr>
</tbody></table>
</body>
</html>
Although I am not yet sure exactly what is wrong - with the help of chris85 I no longer believe it is a PHP problem but instead is probably a hardware problem.
Chris posted to the PHP sandbox what I said I was using and it worked without a flaw. When I tried it ON the PHP sandbox (which means it used the sandbox's PHP interpreter) - it also worked without a problem.
Then I modified the program so it did the same thing but in two other ways. Thus, test #1 was just the single string, test #2 was the exploded string, and test #3 echoed out each individual string as it was modified.
On >MY< machine, the third method caused unexpected output to be generated. As stated though, the PHP sandbox computer did NOT have anything strange happen to it. Thus, this means that there is a hardware problem with my computer and not with PHP.
Sandbox link: http://sandbox.onlinephpfunctions.com/code/f34f57f6521cc6eebcc704b7c127974c0403834d
This question can now be closed.
Related
I'm implementing a quote into a table that has multiple modifications to the string..Trying to figure out how to display the length of each word in a string separated by commas in PHP? In this case it should be displayed in the table like (4,2,3,2,...).
Also, I'm almost certain I don't need to create multiple functions for each modification but I am running low on time and had to make do.
I know this code looks like trash but I've only gotten into PHP/HTML 2 weeks ago..First question here so bear with me.
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Need Help!</title>
</head>
<body>
<center>
<h1></h1>
<table border = "1">
<tr>
<td><b>Modification</b></td>
<td><b>Description</b></td>
</tr>
<tr>
<td>Original</td>
<td><?php orig(quote) ;?></td>
</tr>
<tr>
<td>Capitilize first letter of each word</td>
<td><?php upCase(newquote);?></td>
</tr>
<tr>
<td>Displays the word length for each word seperated by commas</td>
<td><?php ?></td>
</tr>
<tr>
<td>Randomly shuffles each word in the quote</td>
<td><?php ?></td>
</tr>
<?php
function orig($quote){
$quote = " There are only two ways to live your life.
One is as though nothing is a miracle.
The other is as though everything is a miracle. <p>
-Albert Einstein";
echo $quote;
}
function upCase($newquote){
$quote = " There are only two ways to live your life.
One is as though nothing is a miracle.
The other is as though everything is a miracle. <p>
-Albert Einstein";
$newquote = ucwords($quote);
echo $newquote;
}
?>
</table>
</center>
</body>
</html>
The first part is to split the text into words, there are various ways ( explode() by spaces, regex's which split by white space, this just uses str_word_count()).
Then it builds an array of the length of each of the words and implodes() the result...
function wordLengths($quote = "There are only two ways to live your life.
One is as though nothing is a miracle.
The other is as though everything is a miracle. <p>
-Albert Einstein"){
$lengths = [];
foreach ( str_word_count($quote, 1) as $word ) {
$lengths[] = strlen($word);
}
echo implode(",", $lengths);
}
called by
wordLengths();
gives
5,3,4,3,4,2,4,4,4,3,2,2,6,7,2,1,7,3,5,2,2,6,10,2,1,7,1,7,8
Also your existing code contains quite a few errors, try adding...
ini_set('display_errors', 'On');
error_reporting(E_ALL);
at the start of the script to help show problems as they come up.
<tr>
<td>Displays the word length for each word seperated by commas:</td>
<td>
<?php
$string_lengths = [];
foreach(explode(" ","abcd defghi jk lmnop q rstuv") as $each_String){
$string_lengths[] = strlen($each_String);
}
echo implode(",",$string_lengths);
?>
</td>
</tr>
OUTPUT:
Displays the word length for each word seperated by commas: 4,6,2,5,1,5
I'm having a hard time getting the "price" to be displayed. Am I using php the right way in this case?
<html>
<head>
<script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
</head>
<body>
<table width="100%">
<td>Bitstamp</td>
<?php
$url = "https://www.bitstamp.net/api/ticker/";
$fgc = file_get_contents($url);
$json = json_decode($fgc, true);
$price = $json["last"];
?>
<td><?php echo $price; ?></td>
</table>
</body>
</html>
Yes, it's correct. If you don't get reply from file_get_contents function, you might want to set php setting allow_url_fopen=true
http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
I've been working on some code and tested it on localhost, everything works fine, but when i upload it to my server it doesn't work beyond my php tags. No errors are showing either. I have checked both php versions and on localhost i run version 5.4.7 and on server it's version 5.3.21. Maybe this is causing the problem? Is there something I should look for in the phpinfo()? Am I missing something in my code?
Here is the code:
<!DOCTYPE html>
<?php phpinfo(); ?>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<meta charset="utf-8">
<style>
body { background:black;}
.toggle { display:none; }
p {
width:570px;
text-align:justify;
color:#686868;
font-family:Georgia, serif;
}
h2 { color:black; }
button { background:#686868;
border:none;
font-family:Georgia, serif;
width:570px;
}
</style>
</head>
<body>
<?php
include('sql.php');
$i = 0;
while($i < count($rows)) {
echo "
<div>
<button class='trigger'>
<table width='100%'>
<tr>
<td width='20%'><img src='http://localhost/app-side/Icons/bar_icon.png' />
</td>
<td width='80%'><h2>{$rows[$i]['titel']} </h2></td>
</tr>
</table>
</button>
<div class='toggle'>
<p>
{$rows[$i]['info']}
</p>
</div>
</div>";
$i++;
}?>
</body>
<script>
$('.trigger').click(function() {
$(this).siblings('.toggle').slideToggle('fast');
});
</script>
</html>
When i run it, it shows a black background (as it's suppose to) but everything beyond my php starting tag gets cut off.
I have also tried to force my while loop to loop 10 times and also removed the parts where it's getting data from my database to see if it was a mysql related problem. Which I can conclude it's not.
The following line is the problem:
<img src='http://localhost/app-side/Icons/bar_icon.png
The image cannot being loaded as localhost refers to the local computer of the client (where the browser runs) not to your server. Usually such urls in websites are considered malicious ;)
A workaround to make it work on both localhost and server would be to use an relative path. This can be relative to your document or relative to the DOCUMENT_ROOT of your server. I've used the second approach as I don't know the location of your php file on server.
Solution with link relative to DOCUMENT_ROOT:
Replace:
<img src='http://localhost/app-side/Icons/bar_icon.png
by
<img src='/app-side/Icons/bar_icon.png
The only thing that I can assume is that $rows is empty and hence the following code does not get run:
// count($rows) could be equal to 0
while($i < count($rows)) { // this condition becomes false in that case
echo "
<div>
<button class='trigger'>
<table width='100%'>
<tr>
<td width='20%'><img src='http://localhost/app-side/Icons/bar_icon.png' />
</td>
<td width='80%'><h2>{$rows[$i]['titel']} </h2></td>
</tr>
</table>
</button>
<div class='toggle'>
<p>
{$rows[$i]['info']}
</p>
</div>
</div>";
$i++;
}
UPDATE
Based on your comment, it could then due to something inside sql.php, most probably database connection problem. Try putting error_reporting(E_ALL) at the very top of the page and see if that prints out any error.
Have you checked your sql.php to make sure you've changed it to match your live server (changed from localhost variables) also check your database field names versus your script for typos.
[code]
{$rows[$i]['titel']} </h2></td>
[\code]
Is it supposed to read :
[code]
{$rows[$i]['title']} </h2></td>
[\code]
If your database field name is "title" and
You are trying to get data from a field called
"titel" than that could cause your error.
I hope that helps
I'm trying to convert my sample HTML output into a plain text but I don't know how. I use file_get_contents but the page which I'm trying to convert returns most like the same.
$raw = "http://localhost/guestbook/profiles.php";
$file_converted = file_get_contents($raw);
echo $file_converted;
profiles.php
<html>
<head>
<title>Profiles - GuestBook</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<!-- Some Divs -->
<div id="profile-wrapper">
<h2>Profile</h2>
<table>
<tr>
<td>Name:</td><td> John Dela Cruz</td>
</tr>
<tr>
<td>Age:</td><td>15</td>
</tr>
<tr>
<td>Location:</td><td> SomewhereIn, Asia</td>
</tr>
</table>
</div>
</body>
</html>
Basically, I trying to echo out something like this (plain text, no styles)
Profile
Name: John Dela Cruz
Age: 15
Location: SomewhereIn, Asia
but i don't know how. :-( . Please help me guys , thank you in advance.
EDIT: Since i am only after of the content of the page, no matter if it's styled or just a plain text , is there a way to select only (see code below) using file_get_contents() ?
<h2>Profile</h2>
<table>
<tr>
<td>Name:</td><td> John Dela Cruz</td>
</tr>
<tr>
<td>Age:</td><td>15</td>
</tr>
<tr>
<td>Location:</td><td> SomewhereIn, Asia</td>
</tr>
</table>
Use php strip_tags
If strip_tags is not working for then maybe you can use regex to extract the info you want.
Try using PHP preg_match with /(<td>.*?<\/td>)/ as the pattern
Have a look at simplexml_load_file():
http://www.php.net/manual/en/function.simplexml-load-file.php
It will allow you to load the HTML data into an object (SimpleXMLElement) and traverse that object like a tree.
try to use PHP function strip_tags
try this one,
<?php
$data = file_get_contents("your_file");
preg_match_all('|<div[^>]*?>(.*?)</div>|si',$data, $result);
print_r($result[0][0]);
?>
I have try this one, and it seems work for me, for you too i hope
You can use the strip_tags php function for this. Browse through the comments in the php manual page of the strip_tags function to see how you can use this in a good way.
My PHP is not PHPing, so made simple test... must be missing something obvious.
<html>
<head>
</head>
<body>
<?php echo '<script language=\"javascript\">confirm("Do you see this?")</script>;'; ?>
</body>
</html>
In code body, I get: confirm("Do you see this?");'; ?>
When I "View Source", I see:
<html>
<head>
</head>
<body>
<?php echo '<script language=\"javascript\">confirm("Do you see this?")</script>;'; ?>
</body>
</html>
what extension has your file? is a webserver running? how are you calling your php script?
make sure it has a .php extension, the webserver is running, the file resides under the webroot directory and you call it via http://localhosti/path/to/file.php
also make sure you don't escape quotation marks when not needed, echo '<script type="text/javascript">…</script>'; should do the job
You should remove the backslashes from the \"javascript\".
<?php echo '<script language="javascript">confirm("Do you see this?")</script>;'; ?>
In PHP you can put strings in single ' or double " quotes. This is quite hard to explain (and/or understand) in a few lines, so here's a few valid ways to write down a string containing quotes:
echo 'This has "double quotes"...';
echo 'This has \'single quotes\'...';
echo "This has \"double quotes\"...";
echo "This has 'single quotes'...";
There are many more subtleties to this, but this should get you started.
Take out the \ from the double quotes and the extra semi colon
<html>
<head>
</head>
<body>
<?php echo '<script language="javascript">confirm("Do you see this?")</script>'; ?>
</body>
</html>