combine 2 strings in one with some elements [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
The code looks like this:
$link .= '/'. . 'elevi' . .'/'. $clasa . '/' . $litera . '/' . $nume .'-'. $prenume;
But the error apear :
Parse error: syntax error, unexpected '.' in ...
But if the code looks like this :
$link .= $clasa . '/' . $litera . '/' . $nume .'-'. $prenume;
Works like this :
11/c/bobo-alex
and i want it to looks like this
./elevi/11/c/bobo-alex
with [dot] and slash first

You have to many string combining dots in your first segment.
You can also use "" instead of '' for PHP to auto-echo variable contents to the string.
$link .= "./elevi/$clasa/$litera/$nume-$prenume";

The dots are used to concatenate two strings, so you can't put two dots consecutive, it will rise a syntax error, so you can do it in two ways:
$mystring = './' . $var1 . '/' . $var2 . '/' . $var3 . '-' . $var4;
or:
$mystring = "./$var1/$var2/$var3-$var4";

Related

Get PATH instead of URL [duplicate]

This question already has answers here:
File path without domain name from wp_get_attachment_url()
(4 answers)
Closed 10 months ago.
This code:
$files[$file->ID]['file_title'] = '<p><a target="_blank" href="' . esc_url( wp_get_attachment_url( $file->ID ) ) . '">' . $real_title . '</a>' . $title . '</p>';
Gives me:
https://example.com/wp-content/uploads/2022/03/sample.pdf`
But I want:
/wp-content/uploads/2022/03/sample.pdf`
How do I remove https://example.com/?
You can use parse_url to get the path information from a URL:
parse_url(wp_get_attachment_url($file->ID) , PHP_URL_PATH)
https://3v4l.org/5KmsJ
Alternatively str_replace or preg_replace could be used to remove https://example.com/ that those can result in incorrect results.

PHP append to a new line in a file [duplicate]

This question already has answers here:
How to insert a new line into a CSV file?
(4 answers)
Closed 2 years ago.
This is my code in PHP:
$singles = fopen("singles.txt", "a");
$user_info = $name . "," . $gender . "," . $age . "," . $personality . "," .
$favOS . "," . $minAge . "," . $maxAge . PHP_EOL;
fwrite($singles, $user_info);
When I try to append the first time, is it appended to the same line but after that it is going to new line like I want.
As you see, New11 is added to the same line. I want it to be appended to a new line.
Also there is a trailing new line and I don't want an empty line at the end.
Just start the string with a newline, instead of finishing it with one. This should solve both issues - your first append will always start a new line, and your last one won't add a blank newline at the end.
$user_info = PHP_EOL . $name . "," . $gender . "," . $age . "," . $personality . "," .
$favOS . "," . $minAge . "," . $maxAge;

preg_replace vs preg_replace_callback

I'm trying to update the code for some very old plugins for a very old blog. I've fixed almost everything except this.
I get an error message that I must replace preg_replace with preg_replace_callback.
This is the code:
$source_content = preg_replace($search.'e', "'"
. $this->_quote_replace($this->left_delimiter) . 'php'
. "' . str_repeat(\"\n\", substr_count('\\0', \"\n\")) .'"
. $this->_quote_replace($this->right_delimiter)
. "'"
, $source_content);
If I simply substitute preg_replace_callback for the preg_replace I get this error:
preg_replace_callback(): Requires argument 2, ''{{php' . str_repeat(" ", substr_count('\0', " ")) .'}}'', to be a valid callback in
I'm neither a perl nor a php person. Any help would be much appreciated!
Upgrading your smarty library to the latest smarty2 would solve your problem fix was there over 5 years now.
But This might help you :
$source_content = preg_replace_callback($search . 'e',
function ($matches) {
return "'"
. $this->_quote_replace($this->left_delimiter) . 'php'
. "' . str_repeat(\"\n\", substr_count('$matches[0]', \"\n\")) .'"
. $this->_quote_replace($this->right_delimiter) . "'";
},
$source_content
);
Here is the explanition and more examples :
https://www.php.net/manual/en/function.preg-replace-callback.php

Parse error: syntax error, unexpected '"' [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I can's seem to figure out why I'm getting this error.
I need to turn user_link part into hyperlink(right now it outputs a text on the frontend). Error happens on this line in my shortcode.php file. I guess there is an error with a href part:
$output .= '<td>' . $order->billing_first_name . ' ' . $order->billing_last_name . ' <a href='" . $order->user_link . "'>" . $order['user_link'] . "</a> </td>';
Your issue is right here:
. ' <a href='" .
Notice how you're starting with a single quote and ending with a double.
Definitely consider using an IDE. It will make these easy bugs blatantly obvious. I highly recommend PHPStorm, or if you don't want to spend anything, sublime text (Not an IDE but will provide linting and highlighting).
On top of that I'd recommend using some type of templating engine eventually. You should always try to avoid writing HTML as strings.
$output .= '<td>' . $order->billing_first_name . ' ' . $order->billing_last_name . ' ' . $order['user_link'] . ' </td>';
You switched the ' and " in the a href. I also changed the two " to ' in the a description.

PHP Echo post parameter surrounded by strings [duplicate]

This question already has answers here:
Print string with a php variable in it
(4 answers)
Closed 11 months ago.
I have a textbox named 'fname'. I need to echo out the input of this box inside double quotes on a another page.
User enters: Test123
returrns: "Test123"
so how can I do that with $_POST["fname"] ?
Or you can use:
echo "Hi how are you {$_POST['fname']} ? I am fine thanks";
If you want to use it in a string surrounded by letters, simply use curly brackets {}.
Put the $_POST['fname'] to curly brackets.
Try
<?php echo('"'.htmlspecialchars ($_POST["fname"]).'"'); ?>
There are many ways:
besides the one nyarathotep mentioned:
echo sprintf('"%s"', $_POST['fname']);
printf('"%s"', $_POST['fname']);
Use this:
echo '"' . $_POST["fname"] . '"';
Or
echo "'" . $_POST["fname"] . "'";
of course if you want to you can replace ' and " with " or ' in the code...

Categories