Syntax error: Unexpected '/' error - php

I'm using fopen() but I get this error on execution.
Parse error: syntax error, unexpected '/' in /home/furtherpath.. on line 7
The line 7 is:
/home/a11*****/public_html/rishi/rishi_someone_php.php = fopen("rishi_someone_php.php","r");
I know I should place a file handler instead but that also doesn't seem to work and gives the same error.
Can't figure out why.
What I'm doing is creating a html page using php.:
$html="<html> \n <head> \n <title>".$fn."</title> \n </head> \n <body> \n <?php \n $file=fopen('".$full."','r'); \n while(!(feof($file))) \n { \n echo htmlspecialchars(fgets($file)).'<br/>'; \n } \n fclose($file); \n ?> \n </body> \n </html>";
Thanks in advance.

Here is a cleaned up version of your script. Try to understand it. I'e combined the string at each html element just to be clear. There is no reason for php tags within php tags. In your code at this line $file=fopen('".$full."','r');
You are trying to quote a variable of type string. That is not necessary with variables. PHP will know that it is a string, so don't do that. Only use quotes when a string literal is being passed into the function as an arguments.
$html = "<html>\n<head><title>". $fn."</title>\n</head><body>\n";
$fp = fopen($full, "r");
if ($fp) {
while (($line = fgets($fp)) !== false) {
$html .= htmlspecialchars($line, ENT_QUOTES, 'UTF-8');
$html .= "<br />";
}
} else {
$html .= "No data";
$html .= "<br />";
}
fclose($fp);
$html .= "</body>\n</html>\n";
echo $html;

Related

UTF Regex with preg_match in PHP

I need a regeular expression for german words with ä,ü etc.
When I test this regex on this website https://regex101.com/
/^\p{L}+$/u
all is fine, but on my server I upload a CSV and want to parse the words.
When I call with the word "Benedikt"
preg_match("/^[\p{L}]+$/u", $attributes[0])
I get false. The encoding of the CSV is UTF-8, when I convert it to ANSI, all is good but the ä,ü etc. is not shown correctly, so I think I should convert it to UTF-8.
But why is it returning false?
The problem occurs because your csv file starts with a UTF-8 BOM. If you remove this, the regex works perfectly. I have confirmed it with this code:
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<?php
function remove_utf8_bom($text)
{
$bom = pack('H*','EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}
$csvContents = remove_utf8_bom(file_get_contents('udfser_new.csv'));
$lines = str_getcsv($csvContents, "\n"); //parse the rows
foreach ($lines as &$row) {
$row = str_getcsv($row, ";");
$firstName = $row[0];
$lastName = $row[1];
echo 'First name: ' . $firstName . ' - Matches regex: ' . (preg_match("/^[\p{L}]+$/u", $firstName) ? 'yes' : 'no') . '<br>';
echo 'Last name: ' . $lastName . ' - Matches regex: ' . (preg_match("/^[\p{L}]+$/u", $lastName) ? 'yes' : 'no') . '<br>';
}
?>
</body>
</html>
The regex match the text successfully, and the ü in Glückmann is shown correctly on the page.
preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.
http://php.net/manual/en/function.preg-match.php

PHP function for removing &nbsp and skip a line

I would like to erase &nbsp in Wordpress post when we skip a line but keeping the "skip a line".
In post.php I've added this function :
function remove_empty_lines( $content ){
$content = preg_replace("/ /", "\n", $content);
return $content;
}
add_action('content_save_pre', 'remove_empty_lines');
but the \n doesn't work, what can I write for that works ? (<br /> doesn't work too).
\n doesn't represent a new line in HTML, so you won't see the line break when you echo the result. Either use "<br />" directly as the replacement, or use the default nl2br() PHP function to insert HTML line breaks before PHP line breaks e.g.
$sample = "testing a \n line break";
echo $sample;
// HTML output is:
"testing a line break"
$sample2 = "testing a <br /> line break";
echo $sample2;
// HTML output is:
"testing a
line break"
$sample3 = "testing a \n line break";
$sample3 = nl2br($sample3);
echo $sample3;
// HTML output is:
"testing a
line break"
Newlines in HTML are expressed through <br > or <br/>, not through \n.
$content = str_replace(" ", '<br/>', $content);
echo nl2br($content);

Print PHP string in new lines

Anything wrong with this code? I want it to print the name and address - each on a separate line, but it all comes up in one line.
Here's the code
<?php
$myname = $_POST['myname'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$address3 = $_POST['address3'];
$town = $_POST['town'];
$county = $_POST['county'];
$content = '';
$content .="My name = " .$myname ."\r\n";
$content .="Address1 = " .$address1 ."\n";
$content .="Address2 = " .$address2 ."\n";
$content .="Address3 = " .$address3 ."\n";
$content .="town = " .$town ."\n";
$content .="county = " .$county ."\n";
echo $content;
?>
It looks like the '\n' character is not working.
In your source code this will show on a next line, but if you want to go to another line in HTML you will have to append <br />.
So:
$content .="My name = " .$myname ."<br />\r\n";
I left the \r\n here because it will go to the next line in your source code aswell, which might look nicer if you have to view the source.
The \n character properly works just fine. The problem is, it's not what you expect.
If you see this in a browser, you won't see line breaks, because line breaks are ignored in the source code. The HTML parser only reads <br> as line breaks.
If you try to go to your website and view the source code, you'll find that the line breaks are in there.

PHP, Line break when writting to a file

I am trying to keep a running total of all the responses to a form I have written, but I am having trouble making it so that each response takes a new line. I have my code down below. I just want it so that it is easier to read because right now what happens is that all the responses are jammed together would like to have each one on a new line. I tried a few things and have them commented in the code and what the result was. Thanks in Advance.
<?php
if (isset($_POST['sometext']))
{
$myFile = "testFile.txt";
$thetext=$_POST['sometext'] ;//added + "\n" here but all response turned to 0
writemyfile($myFile,$thetext,"a");
} else
{
$thetext="Enter text here";
}
function readmyfile($thefile)
{
$file = fopen($thefile, "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
}
function writemyfile($thefilename,$data,$mode)
{
$myfile=fopen($thefilename,$mode);
fwrite($myfile, $data); // added + "\n" here and responses turned 0
fclose($myfile);
}
?>
<html>
<head>
<title> Zain's Test Site</title></head>
<body>
<form method="post" action="<?php echo $php_self ?>">
<input type="text" name="sometext" value="<?php echo $thetext ?>" >
<input type="submit" name="Submit" value="Click this button">
</form>
<?php readmyfile("testFile.txt"); ?>
</body>
Can you try appending the newline character (\n) to the $thetext variable like this:
$thetext=$_POST['sometext'] . "\n";
Remember to use '.' as the concatenation operator, and use double-quotes around the newline character.
$thetext."\n"
in php you concatenate strings using ".", you use "+" in javascript.
Use newline "\n" instead of the br's which is for html
$text = $text."\n" ?
Err here's some more text to fill out the answer
fwrite($myfile, $data); // added + "\n" here and responses turned 0
the concat string operator is (.) not (+)
you can also simplify your script thusly
echo nl2br(get_file_contents($file));

Elegant solution for line-breaks (PHP)

$var = "Hi there"."<br/>"."Welcome to my website"."<br/>;"
echo $var;
Is there an elegant way to handle line-breaks in PHP? I'm not sure about other languages, but C++ has eol so something thats more readable and elegant to use?
Thanks
For linebreaks, PHP as "\n" (see double quote strings) and PHP_EOL.
Here, you are using <br />, which is not a PHP line-break : it's an HTML linebreak.
Here, you can simplify what you posted (with HTML linebreaks) : no need for the strings concatenations : you can put everything in just one string, like this :
$var = "Hi there<br/>Welcome to my website<br/>";
Or, using PHP linebreaks :
$var = "Hi there\nWelcome to my website\n";
Note : you might also want to take a look at the nl2br() function, which inserts <br> before \n.
I have defined this:
if (PHP_SAPI === 'cli')
{
define( "LNBR", PHP_EOL);
}
else
{
define( "LNBR", "<BR/>");
}
After this use LNBR wherever I want to use \n.
in php line breaks we can use PHP_EOL (END of LINE) .it working as "\n"
but it cannot be shown on the ht ml page .because we have to give HTML break to break the Line..
so you can use it using define
define ("EOL","<br>");
then you can call it
I ended up writing a function that has worked for me well so far:
// pretty print data
function out($data, $label = NULL) {
$CLI = (php_sapi_name() === 'cli') ? 'cli' : '';
$gettype = gettype($data);
if (isset($label)) {
if ($CLI) { $label = $label . ': '; }
else { $label = '<b>'.$label.'</b>: '; }
}
if ($gettype == 'string' || $gettype == 'integer' || $gettype == 'double' || $gettype == 'boolean') {
if ($CLI) { echo $label . $data . "\n"; }
else { echo $label . $data . "<br/>"; }
}
else {
if ($CLI) { echo $label . print_r($data,1) . "\n"; }
else { echo $label . "<pre>".print_r($data,1)."</pre>"; }
}
}
// Usage
out('Hello world!');
$var = 'Hello Stackoverflow!';
out($var, 'Label');
Not very "elegant" and kinda a waste, but if you really care what the code looks like you could make your own fancy flag and then do a str_replace.
Example:<br />
$myoutput = "After this sentence there is a line break.<b>.|..</b> Here is a new line.";<br />
$myoutput = str_replace(".|..","<br />",$myoutput);<br />
or
how about:<br />
$myoutput = "After this sentence there is a line break.<b>E(*)3</b> Here is a new line.";<br />
$myoutput = str_replace("E(*)3","<br />",$myoutput);<br />
I call the first method "middle finger style" and the second "goatse style".
Because you are outputting to the browser, you have to use <br/>. Otherwise there is \n and \r or both combined.
Well, as with any language there are several ways to do it.
As previous answerers have mentioned, "<br/>" is not a linebreak in the traditional sense, it's an HTML line break. I don't know of a built in PHP constant for this, but you can always define your own:
// Something like this, but call it whatever you like
const HTML_LINEBREAK = "<br/>";
If you're outputting a bunch of lines (from an array of strings for example), you can use it this way:
// Output an array of strings
$myStrings = Array('Line1','Line2','Line3');
echo implode(HTML_LINEBREAK,$myStrings);
However, generally speaking I would say avoid hard coding HTML inside your PHP echo/print statements. If you can keep the HTML outside of the code, it makes things much more flexible and maintainable in the long run.
\n didn't work for me. the \n appear in the bodytext of the email I was sending.. this is how I resolved it.
str_pad($input, 990); //so that the spaces will pad out to the 990 cut off.

Categories