Shortening a clickable url stored inside a variable - php

This is my current code:
$parcels = $api->parcels->get();
$url = (array_values($parcels)[0]['label']['label_printer']);
$goToUrl = $api->getUrl($url);
$goToUrl = str_replace('/api/v2//api/v2/', '/api/v2/', $goToUrl);
print_r($goToUrl);
echo "<br />";
echo $url;
Why do I use str_replace()? because I am intending to redirect to $goToUrl and this is not working because the current API is giving me the link wrong.
This is my output:
https://api_key:api_secret#panel.sendcloud.nl/api/v2/labels/label_printer/1369315
Code where it's used in the email:
$email_body = ("This is Label: " . ($parcel_name) . " |OrderId: " . ($parcel_order_number) . "\n\n See Label: " . $goToUrl );
Current situation this url get's emailed, and in the mail I would like it to be a clickable link instead of the entire url. Even only maybe the last part is good enough:
label/label_printer/1369315
i've seen preg_replace but never used it before so I find it very difficult to understand it.

To enable HTML in phpmailer you have to use
$mailer->isHtml(true); //Enables html in the regular body.
Or
$mailer->MsgHTML($body); //Enables and SETS the message body with HTML.
You can see http://phpmailer.worxware.com/ for examples / guides on how to use phpmailer with html / configure it properly.

Related

Send parameter hashtag to another page in php

I'm having trouble with send parameter with value hashtag (#) in a HTTP request.
Example I have matcontents is #3232, then i need to sent parameter wtih that value. I've tried doing this:
echo "<td width=25% align=left bgcolor=$bgcolor id='title'> <font face='Calibri' size='2'>
<a href='../report/rptacc_det.php?kode=$brs3[matcontents]&fromd=$fromd2&tod=$tod2&type=$type&fty=$fty&nama=$brs3[itemdesc]' target='_blank'>
$brs3[matcontents]</a></font></td>";
But when i call kode on rptacc_det.php, i got nothing or blank. How do i send value like "#3232" to another page??
Anything after a # is not sent to the server in a request as it's interpreted as an anchor location on the page.
It is possible to send them, but you need to urlencode them.
$kode = "this is a #test";
// Does not work:
// In the following, $_GET['parameter'] will be "this is a ";
// link will be '?kode=this is a #test'
echo '<a href=../report/rptacc_det.php?kode' . $kode . '>Click</a>';
// Works:
// In the following, $_GET['parameter'] will contain the content you need
// link will be '?kode=this%20is%20a%20%23test'
echo '<a href=../report/rptacc_det.php?kode' . urlencode($kode) . '>Click</a>';
To get the value back in rptacc_det.php, you can use urldecode
$kode = urldecode($_GET['kode']);
You should do this to all variables which you are including in a URL.

build link using postgresql and php

)
I have a question:
I have a databse with the following information:
Name of report [name]
Link to report [link]
The various reports are displayed in tables in my page (using db_tables filed with static, month, week etc)
But i want to generate a link to the report based on "link" and "name"
so i set up a pgsql query :
result = select name, link from db where type = 'week'
This works fine.
To display the above i have:
echo "<div id=\"selMaand\">" ;
echo "<table id = \"t2\">\n ";
echo "\t<tr class=\"head\">\n";
echo "<th colspan=\"2\">Select Maand</th>";
while ($line = pg_fetch_array($result5, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td width=\"100%\">$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
echo "</div>";
which works fine to.
Now, the link from database is http://192.168.178.1:8080/etc/etc/page.html"onclick humptydumpty>name
each time i want to adjust something in either the onlclick or address, i have to change all my links.
What can i insert in my querys so i will be able to build a link like;
echo "<a href = "http://192.168.178.1:8080/etc/etc/**$link**"onlclick humptydumpyu>**$name**</a>
I cant figure out how to break down my array!
Thanks in advance
Sjoerd
If you just need a portion of your URL in the database (i.e. only a portion of it would change for each record), then only store that portion of the URL in the database. You can then generate the remaining portion of the URL (call it the URL base) in your code.
So something like this:
define('URL_BASE', 'http://some.url.base/that/you/can/change/globally/');
Then you get the remaining piece of the URL from the database for each item. So you would have something like this inside your query loop to build the full URL.
$link = $line['link'];
$final_url = URL_BASE . $link;
As far as the onclick stuff goes, I would have that set sepearately as well, since I assume the onclick behavior is not dependent on the individual link (if it is you can make a separate DB column for it).
define('LINK_ONCLICK', 'onclick="some_onclick_function()"');
And in your query loop you can put this together like this:
$name = $line['name'];
$link = $line['link'];
$final_url = URL_BASE . $link;
echo '<a href="' . $final_url . '" ' . LINK_ONCLICK . '>' . $name . '</a>';
Note that it is not really necessary to define the strings for the url base and onclick behavior as constants. I just showed it this way as it is common practice to set globally defined values that do not need to change at run-time ion such a manner.

How do I get fwrite() to not print to the page?

I'm using fwrite() to write a file, however, every time it runs, it prints all of the information to the webpage it's on and writes to the file, too. I'd rather it not echo it to the page.
Alright, I've retrieved the code. Here it is:
fwrite($data_file, "Confirmation Data: \r\n\r\n Song: " . $input_song . "\r\nFile1: " . $file_var1 . "\r\nFile2: " . $file_var2 . "\r\nFile3: " . $file_var3 . "\r\nFile4: " . $file_var4 . "\r\nExplanation Text: " . $input_explanation);
Whenever I run this, it outputs everything in the second place of fwrite() onto the page, so it outputs the follow onto the page, except with the variables replaced for their values:
Confirmation Data: \r\n\r\n Song: " . $input_song . "\r\nFile1: " . $file_var1 . "\r\nFile2: " . $file_var2 . "\r\nFile3: " . $file_var3 . "\r\nFile4: " . $file_var4 . "\r\nExplanation Text: " . $input_explanation
You should post your code, so we can follow it but most probably you are also echoing out the code as HTML in your PHP file and this is why it prints out on the screen.
You said your information is being echoed and not written to file. Perhaps you are using echo instead of using file related functions. However, without access to your code, I can only post how to write information to a file in PHP:
<?php
error_reporting(E_ALL);//display all errors
ini_set('display_errors', true);
$filename = "testfile.txt";//the name of your output file
$fh = fopen($myFile, 'w');//create a filehandler - fh) and open the file in write mode
$string = "ABCDEFGH\n";//test string to write to the file
$myvar = fwrite($fh, $string);
$string = "IJKLMNO\n";//update the string variable with new data
fwrite($fh, $string);//write more data - can be repeated ad -infinitum
fclose($fh);//finally, close the file
But, if you truly want definitive answers, post your code so that we may be able to help you.

Adding extra content to wp_mail 'message' segment

I'm using a Custom Wordpress Template which is using the wp_mail Function.
So I have this code below, and the bolded portion is giving me problems:
wp_mail($et_email_to, sprintf( '[%s] ' . esc_html($_POST['et_contact_subject']), $et_site_name ), esc_html($_POST['et_contact_message']),'From: "'. esc_html($_POST['et_contact_name']) .'" <' . esc_html($_POST['et_contact_email']) . '>');
I want to include extra stuff where the Message portion is. esc_html($_POST['et_contact_message'])
When I try to add anything extra there, it gives various PHP errors.
If I replace that esc_html() part with normal text like '12345', it works fine.
What I want to add to the end of my messages is this:
"IP Address: ".$_SERVER['REMOTE_ADDR']."
Came From: ".$_SERVER['HTTP_REFERER']."
Browser: ".$_SERVER['HTTP_USER_AGENT']
It works fine if I remove that esc_html() portion, but I can't get both to work together.
esc_html() works fine by itself also, it prints the Message from the Contact form.
I just can't get both things to work together.
These will fail for example:
esc_html($_POST['et_contact_message']) 12345,
esc_html($_POST['et_contact_message'] $_SERVER['REMOTE_ADDR']),
etc...
Please help me out here.
Thanks!
Check out string concatenation in PHP - there are many ways to do it, I'll highlight a few from http://www.php.net/manual/en/language.types.string.php
<?php
$foo = 'some';
$bar = 'baz';
$message = '';
$message .= 'Add ' . $foo;
$message .= ' variable to a string';
// You can do this all at once, in one line or many
$mesage = 'Add ' . $foo . ' variable to a string';
// Double-quotes are useful
$message = "Add {$foo} variable to a string";
// And there are other methods, like HEREDOC
$message = <<<TEXT
Add $foo variable to a string...in a HEREDOC
block.
TEXT;
// Some editors, like VIM, support syntax highlighting in HEREDOC
// Useful if you want to write out legeble code from other languages, like
// SQL.
$message = <<<SQL
SELECT
*
FROM
table
WHERE
col = %s
SQL;
$wpdb->prepare($message, $foo);
I finally found a way to make it work!
Here is the code:
$message = $_POST['et_contact_message'];
$message .= '
IP Address: ' . $_SERVER['REMOTE_ADDR'];
$message .= '
Came From: ' . $_SERVER['HTTP_REFERER'];
$message .= '
Browser: ' . $_SERVER['HTTP_USER_AGENT'];
wp_mail($et_email_to, sprintf( '[%s] ' . esc_html($_POST['et_contact_subject']), $et_site_name ), $message,'From: "'. esc_html($_POST['et_contact_name']) .'" <' . esc_html($_POST['et_contact_email']) . '>');

return json string with html characters?

i want to call a php with jquery ajax to perform some database things and then return 2 lists of links. so i have to pass these two link lists back to jquery so it can display list 1 in the left side of the webpage and list 2 in the right side.
i created the lists in separate arrays that i send back to jquery with json_encode but i noticed that it escapes all the html characters.
<a>dog</a> became <a>dog<\/a>
so when i displayed the list in the html they werent links anymore.
how can i preserve the html codes in my returned arrays to jquery?
EDIT: is this the right way to go if you want to split up data from php so that jquery can display them in different locations in html?
// list 1
while($row = mysqli_fetch_assoc($saved_tags))
{
$display_saved_tags[] = "<a id='showtag' href='answer.php?id=" . $row['id'] . "'>" . $row['name'] . "</a><br />";
}
// list 2
while($row = mysqli_fetch_assoc($related_tags))
{
$display_related_tags[] = "<a id='showtag' href='answer.php?id=" . $row['id'] . "'>" . $row['name'] . "</a><br />";
}
// return lists to jquery
echo json_encode('display_saved_tags' => $display_saved_tags, 'display_related_tags' => $display_related_tags));
json_encode's escape characters directly conflict with HTML output. I have had the same issue but decided to use an alternative solution at the time. I just had a thought that you could perhaps do this:
$data = new stdClass();
$data->html1 = base64_encode('<h1>html in here</h1>');
$data->html2 = base64_encode('<p><strong>more html</strong></p>');
echo json_encode($data);
On the frontend:
callbackFunction(json) {
alert(base64_decode(json.html1));
alert(base64_decode(json.html2));
}
You would need the javascript implementations of base64_decode and utf8_decode which can be found at:
http://phpjs.org/functions/base64_decode:357
use can use below function to un-escape the chars when reading or sending on to the browser:
html_entity_decode('your response here');
Also because your are using json_encode, make sure that you don't need the below function in your code:
json_decode

Categories