PHP file_get_contents then replace string in a function - php

**User_Tpl.html**
<html><head></head>
<body>
Hello {NAME},
Business name : {BUSINESS_NAME}.
Tel : {TELEPHONE}.
Mob : {MOBILE}.
</body>
</html>
function mailFooter($content) {
$sBusiness = 'Business name';
$sTelp = '0';
$sMobile = '0';
$content = str_replace('{BUSINESS_NAME}', $sBusiness, $content);
$content = str_replace('{TELEPHONE}', $sTelp, $content);
$content = str_replace('{MOBILE}', $sMobile, $content);
return $content;
}
$content = file_get_contents('page/email/User_Tpl.html');
$content = str_replace('{SUBJECT}', $subject, $content);
$content = str_replace('{NAME}', $ownerName, $content);
**mailerFooter($content);**
// always return {BUSINESS_NAME}, {TELEPHONE}
$mail->AddAddress( $email );
$mail->SetFrom($name );
$mail->AddReplyTo( $reply );
$mail->Subject = trim($subject);
$mail->MsgHTML( trim($content) );
$mail->IsHTML(true);
$mail->Send();
im using PHPMailer as Mailer library and how to replace those {strings} inside the mailFooter() function.

use array
$content ='
<html><head></head>
<body>
Hello {NAME},
Business name : {BUSINESS_NAME}.
Tel : {TELEPHONE}.
Mob : {MOBILE}.
</body>
</html>';
$search= array ('{BUSINESS_NAME}','{TELEPHONE}','{MOBILE}');
$replace=array($sBusiness,$sTelp,$sMobile);
$content =str_replace($search,$replace,$content);

private arr = array(
"{title}" => "Home page",
"{email}" => "my#mail.com",
...
);
private $content = "Title for my site - {title}, if you interesting my email addres -> {email}"
$this->content = strtr($this->content, $this->arr);
echo $this->content;
This return -> "Title for my site - Home page, if you interesting my email addres -> my#mail.com"

Related

How to exclude image extension from string place in PHP

I want to replace all the string values from example.com to sample.com excluding the image extension .png, .jpeg, .jpg etc.
$body = ' www.example.com, example.com, example.com/hello.html, example.com/logo.png, example.com/yourbaby.php , example.com/newimage.jpg, www.example.com/newimage.jpg';
//values
$oldomain = ['example.com','www.'];
$newdomain= ['sample.com', ''];
$excludeextension = [".png", ".jpg", ".jpeg"];
//Replace
$body = str_replace($olddomain, $newdomain, $body);
$body = str_replace($excludeextension, '', $body);
//output
echo $body;
Output i am looking for :
sample.com, sample.com, sample.com/hello.html, example.com/logo.png, sample.com/yourbaby.php , example.com/newimage.jpg, www.example.com/newimage.jpg
Expectation
https://example.com -> https://sample.com
https://www.example.com -> https://sample.com
https://subdomain.example.com -> https://subdomain.sample.com
https://www.example.com/image.png -> https://www.example.com/image.png
https://example.com/image.png -> https://example.com/image.png
You can do this using str_replace() and a bit of exploding and imploding.
$body = ' www.example.com, example.com, example.com/hello.html, example.com/logo.png, example.com/yourbaby.php, example.com/newimage.jpg, www.example.com/newimage.jpg';
$oldomain = ['example.com','www.'];
$newdomain= ['sample.com', ''];
$excludeextension = ["png", "jpg", "jpeg"];
$doms = explode(',', $body);
foreach ($doms as &$dom) {
// get extension
$pi = pathinfo($dom);
if ( ! in_array( $pi['extension'], $excludeextension) ){
$dom = str_replace($oldomain, $newdomain, $dom);
}
}
$NewStr = implode($doms);
echo $NewStr;
RESULT
sample.com sample.com sample.com/hello.html example.com/logo.png sample.com/yourbaby.php example.com/newimage.jpg www.example.com/newimage.jpg
Using PATHINFO_EXTENSION & str_replace
First explode than implode
$body = 'example.com, www.example.com, https://www.example.com, https://example.com, subdomain.example.com, example.com/logo.png, www.example.com/logo.png, example.com/hello/logo.png, https://example.com/logo.png, subdomain.example.com/logo.png"';
//Defined Values
$oldomain = ["example.com", "www.example.com"];
$newdomain = "sample.com";
$excludeextension = ["png", "jpg", "jpeg", "css"];
// Split the string into an array of URLs
$urls_body_extension = explode(", ", $body);
// Loop through each URL and replace it if necessary
foreach ($urls_body_extension as &$url_body_extension) {
$extension_body_extension = pathinfo($url_body_extension, PATHINFO_EXTENSION);
if (!in_array($extension_body_extension, $excludeextension)) {
foreach ($oldomain as $domain_body_extension) {
if (strpos($url_body_extension, $domain_body_extension) !== false) {
$url_body_extension = str_replace($domain_body_extension, $newdomain, $url_body_extension);
break;
}
}
}
}
// Join the modified URLs back into a string
$body = implode(", ", $urls_body_extension);
echo $body;

Php mailer missing string from FromName

I am using PHPmailer version 5.2.22 and losing some part of string from FromName.
For e.g.
Mail sent with FromName : "CONC, abcø"
Outputs FromName : "abcø"
String part "CONC," is removed from FromName of mail received.
Code sample :
<?php
$email = 'waleedAhmed#mymail.com';
$name = 'waleed';
$event_info[0]['organizer_name'] = 'CONC, abcø';
$subject = 'Testing..';
$content = ' content : '. $event_info[0]['organizer_name'];
$objName = new \PHPMailer();
$objName->CharSet = 'UTF-8';
$objName->Mailer = "smtp";
$objName->Host = "mail.mymail.com";
$objName->Port = 25;
$objName->Mailer = "sendmail";
$objName->From = 'donotreply#mymail.com';
$objName->Sender = 'donotreply#mymail.com';
$objName->FromName = $event_info[0]['organizer_name']
$objName->IsHTML(true);
$objName->Body = $content;
$objName->Send();
unset($objName);
// ' \"CONC, abcø\" < donotreply#mymail.com > ';
$objName->FromName = '\"'.$event_info[0]['organizer_name'].'\" < donotreply#mymail.com >';
Working fine.

Telegram bot send file and text PHP

I've got a basic bot set up that will send text back to the user, now I also want to send the user an audio message. But that I cannot do, here is the code.
I'm also using this https://github.com/Eleirbag89/TelegramBotPHP to send the audio
include("Telegram.php);
define('BOT_TOKEN', 'tokentoken');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
$telegram = new Telegram(BOT_TOKEN);
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
$reply = sendMessage($message);
$sendto =API_URL."sendmessage?chat_id=".$chatID."&text=".$reply;
file_get_contents($sendto);
function sendMessage(&$string) {
switch ($string) {
case "Hi":
$message = "Hi Back";
sendAudio();
break;
case "Bye":
$message = "Bye Bye";
break;
default:
$message = "Default";
}
return $message
}
func sendAudio() {
$sound = curl_file_create('sampleAudio.mp3', 'audio/mp3');
$newContent = array('chat_id' => $chatID, 'audio' => $sound);
$telegram->sendAudio($newContent);
}
Calling the code outside of the functions works, but than the user gets the file each time they type something. I experimenting so a bit of explanation would be great.
You have some errors:
First line " is unclosed
Last function, you've typed "func" instead of "function"
In sendAudio() you've used $chatID but you've to pass it as a parameter of sendAudio() or to set is as global.

preg_replace on email template

i use swift mailer , and when i get an email from the server i get this :
You request your password via Forget Password
Your username is : {myusername}
Your password is : {mypassword}
with { } , the function for send is :
function format_emailforget($info, $format){
//set the root
$root = $_SERVER['DOCUMENT_ROOT'].'/email';
//grab the template content
$template = file_get_contents($root.'/forget_template.'.$format);
//replace all the tags
$template = preg_replace('{USERNAME}', $info['username'], $template);
$template = preg_replace('{EMAIL}', $info['email'], $template);
$template = preg_replace('{PASSWORD}', $info['password'], $template);
$template = preg_replace('{SITEPATH}','http://smracer.com', $template);
//return the html of the template
return $template;
}
where is the bad think of this code ?
More elegant way and easy to maintain :)
$pattern = array('/\{USERNAME\}/' , '/\{EMAIL\}/' , '/\{PASSWORD\}/' , '/\{SITEPATH\}/');
$replacement = array($info['username'], $info['email'], $info['password'], 'http://smracer.com');
$template = preg_replace($pattern, $replacement, $template);

Using Zend_Http_Client to send json POST

I'm trying to send a json object as a POST command using the following:
$uri = "http://amore-luce.com/product_create";
$product = $observer->getEvent()->getProduct();
$json = Mage::helper('core')->jsonEncode($product);
Mage::log(" Json={$json}", null,'product-updates.txt');
// new HTTP request to some HTTP address
$client = new Zend_Http_Client('http://amore-luce.com/product_create');
// set some parameters
$client->setParameterPost('product', $json);
// POST request
$response = $client->request(Zend_Http_Client::POST);
When I view the $json the data is there and all looks good - however the POST is not sending the json data. I'm capturing it using a simple email form that should send me the response:
<?php
$webhookContent = "";
$ref = "";
$webhook = fopen('php://input' , 'rb');
while (!feof($webhook)) {
$webhookContent .= fread($webhook, 4096);
}
fclose($webhook);
$headers = array();
foreach($_SERVER as $key => $value) {
if (substr($key, 0, 5) <> 'HTTP_') {
continue;
}
$header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
$headers[$header] = $value;
}
foreach ($headers as $header => $value) {
$ref .= "$header: $value <br />\n";
}
$post = file_get_contents('php://input');
$to = "address#my-email.com"; //the address the email is being sent to
$subject = "This is the subject"; //the subject of the message
$msg = "This is the message - Webhook content: ".$webhookContent." This is url: ".$ref; //the message of the email
mail($to, $subject, $msg, 'From: PHP Scriptv2 <noreply#domain.com>'); //send the email.
echo ($_SERVER['HTTP_REFERER']."<br>".$_SERVER['REQUEST_URI']);
?>
This page works with other json POST requests I've sent to it. I'm fairly new at sending POST requests so any help would be greatly appreciated.
Try change :
$client->setParameterPost('product', $json);
to :
$client->setHeaders('Content-type','application/json');
$client->setParameterPost('product', $json);
or use:
$client->setRawData($json, 'application/json');
So Update / Answer changing how I did it to these lines of code:
$uri = "http://requestb.in/p6p4syp6";
$product = $observer->getEvent()->getProduct();
$json = Mage::helper('core')->jsonEncode($product);
Mage::log(" Json={$json}", null,'product-updates.txt');
$client = new Zend_Http_Client($uri);
$client->setRawData($json, null)->request('POST');
Changing the SetRawData($json, null) was the bit - using SetRawDate($json, 'application/json') caused it to fail.
Thanks Voodoo417 - I'll also try your suggestion and see if that lets me set the correct type

Categories