how to use in_array in smarty
in_array in smarty
{if $variable|in_array:[14, 15, 18]}
... Your code
{/if}
Or
in_array in smarty
{if $variable|in_array:$array}
... Your code
{/if}
Convert PDF to Text in PHP
<?php
function pdf2string($sourcefile) {
$fp = fopen($sourcefile, 'rb');
$content = fread($fp, filesize($sourcefile));
fclose($fp);
$searchstart = 'stream';
$searchend = 'endstream';
$pdfText = '';
$pos = 0;
$pos2 = 0;
$startpos = 0;
while ($pos !== false && $pos2 !== false) {
$pos = strpos($content, $searchstart, $startpos);
$pos2 = strpos($content, $searchend, $startpos + 1);
if ($pos !== false && $pos2 !== false){
if ($content[$pos] == 0x0d && $content[$pos + 1] == 0x0a) {
$pos += 2;
} else if ($content[$pos] == 0x0a) {
$pos++;
}
if ($content[$pos2 - 2] == 0x0d && $content[$pos2 - 1] == 0x0a) {
$pos2 -= 2;
} else if ($content[$pos2 - 1] == 0x0a) {
$pos2--;
}
$textsection = substr( $content, $pos + strlen($searchstart) + 2, $pos2 - $pos - strlen($searchstart) - 1 );
$data = @gzuncompress($textsection);
$pdfText .= pdfExtractText($data);
$startpos = $pos2 + strlen($searchend) - 1;
}
}
return preg_replace('/(\\s)+/', ' ', $pdfText);
}
function pdfExtractText($psData){
if (!is_string($psData)) {
return '';
}
$text = '';
// Handle brackets in the text stream that could be mistaken for
// the end of a text field. I'm sure you can do this as part of the
// regular expression, but my skills aren't good enough yet.
$psData = str_replace('\\)', '##ENDBRACKET##', $psData);
$psData = str_replace('\\]', '##ENDSBRACKET##', $psData);
preg_match_all( '/(T[wdcm*])[\\s]*(\\[([^\\]]*)\\]|\\(([^\\)]*)\\))[\\s]*Tj/si', $psData, $matches );
for ($i = 0; $i < sizeof($matches[0]); $i++) {
if ($matches[3][$i] != '') {
// Run another match over the contents.
preg_match_all('/\\(([^)]*)\\)/si', $matches[3][$i], $subMatches);
foreach ($subMatches[1] as $subMatch) {
$text .= $subMatch;
}
} else if ($matches[4][$i] != '') {
$text .= ($matches[1][$i] == 'Tc' ? '' : '') . $matches[4][$i];
}
}
// Translate special characters and put back brackets.
$trans = array( '...' => '…', '\\205' => '…', '\\221' => chr(145), '\\222' => chr(146), '\\223' => chr(147), '\\224' => chr(148), '\\226' => '-', '\\267' => '•', '\\(' => '(', '\\[' => '[', '##ENDBRACKET##' => ')', '##ENDSBRACKET##' => ']', chr(133) => '-', chr(141) => chr(147), chr(142) => chr(148), chr(143) => chr(145), chr(144) => chr(146), );
$text = strtr($text, $trans);
return $text;
}
$sourcefile = '16.pdf';
$get = pdf2string($sourcefile);
echo utf8_encode($get);
php curl request
function makeCURLRequest($url, $method="GET", $params = "") {
if ($method == "GET" && strpos($url, '?')) {
$urlParams = substr($url, strpos($url, '?')+1);
$url = substr($url, 0, strpos($url, '?'));
if (is_array($params)) {
parse_str($urlParams, $urlParamsArray);
$params = $urlParamsArray + $params;
} else {
$params = $urlParams.'&'.$params;
}
}
if (is_array($params)) {
$params = http_build_query($params,'','&');
}
$curl = curl_init($url . ($method == "GET" && $params != "" ? "?" . $params : ""));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HTTPGET, ($method == "GET"));
curl_setopt($curl, CURLOPT_POST, ($method == "POST"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
if ($method == "POST") {
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
}
$response = curl_exec($curl);
return $response;
}
if ($method == "GET" && strpos($url, '?')) {
$urlParams = substr($url, strpos($url, '?')+1);
$url = substr($url, 0, strpos($url, '?'));
if (is_array($params)) {
parse_str($urlParams, $urlParamsArray);
$params = $urlParamsArray + $params;
} else {
$params = $urlParams.'&'.$params;
}
}
if (is_array($params)) {
$params = http_build_query($params,'','&');
}
$curl = curl_init($url . ($method == "GET" && $params != "" ? "?" . $params : ""));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HTTPGET, ($method == "GET"));
curl_setopt($curl, CURLOPT_POST, ($method == "POST"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
if ($method == "POST") {
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
}
$response = curl_exec($curl);
return $response;
}
Magento 2 basic command
1) clear cache : php bin/magento cache:flush
2) reindex : php bin/magento indexer:reindex
3) cmd to deploy static data : php bin/magento setup:static-content:deploy en_US en_AU
4) show which mode is active developer or production
php bin/magento deploy:mode:show
set developer mode : php bin/magento deploy:mode:set developer
set production mode :php bin/magento deploy:mode:set production
5) check module status
php bin/magento module:status
6) Enable module
php bin/magento module:enable Your_modulename --clear-static-content
eg. php bin/magento module:enable Rain_Affiliate --clear-static-content
7) Dissable module
php bin/magento module:disable Magento_Weee
8 ) After install module you have to run this 2 command
php bin/magento setup:upgrade
php bin/magento setup:di:compile
magento 2 regenerate product url php code
please follow the step as i show below
1) backup you database
2) create one controller name Producturl.php
3) copy my code into that controller
<?php
namespace Company\Module\Controller\Producturl;
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\Store\Model\Store;
class Producturl extends \Magento\Framework\App\Action\Action
{
public function execute()
{
//// for regenerate product url /////////////////
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->collection = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Collection');
$this->productUrlRewriteGenerator = $objectManager->create('\Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator');
$this->urlPersist = $objectManager->create('\Magento\UrlRewrite\Model\UrlPersistInterface');
$this->collection->addAttributeToSelect(['url_path', 'url_key']);
$list = $this->collection->load();
$store_id=1; // your store id
foreach($list as $product) {
if($store_id === Store::DEFAULT_STORE_ID)
$product->setStoreId($store_id);
$this->urlPersist->deleteByData([
UrlRewrite::ENTITY_ID => $product->getId(),
UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE,
UrlRewrite::REDIRECT_TYPE => 0,
UrlRewrite::STORE_ID => $store_id
]);
try {
$this->urlPersist->replace(
$this->productUrlRewriteGenerator->generate($product)
);
} catch(\Exception $e) {
$out->writeln('<error>Duplicated url for '. $product->getId() .'</error>');
}
}
}
}
?>
make url short php
This code is for make you url short through google API
<?php
// Declare the class
class GoogleUrlApi {
public $key=your key
// Constructor
function GoogleURLAPI($apiURL = 'https://www.googleapis.com/urlshortener/v1/url') {
// Keep the API Url
$this->apiURL = $apiURL.'?key='.$this->key;
}
// Shorten a URL
function shorten($url) {
// Send information along
$response = $this->send($url);
// Return the result
return isset($response['id']) ? $response['id'] : false;
}
// Expand a URL
function expand($url) {
// Send information along
$response = $this->send($url,false);
// Return the result
return isset($response['longUrl']) ? $response['longUrl'] : false;
}
// Send information to Google
function send($url,$shorten = true) {
// Create cURL
$ch = curl_init();
// If we're shortening a URL...
if($shorten) {
curl_setopt($ch,CURLOPT_URL,$this->apiURL);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode(array("longUrl"=>$url)));
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-Type: application/json"));
}
else {
curl_setopt($ch,CURLOPT_URL,$this->apiURL.'&shortUrl='.$url);
}
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
// Execute the post
$result = curl_exec($ch);
// Close the connection
curl_close($ch);
// Return the result
return json_decode($result,true);
}
}
// Create instance with key
$key = 'AIzaSyDUtkDIQ6uK7GCE68Lji_eQE9lPlo3Lk5Q';
$googer = new GoogleURLAPI();
// Test: Shorten a URL
$shortDWName = $googer->shorten("http://americanentrepreneurship.com/nj/entrepreneurs-featured/university-entrepreneurial-activities-draw-alumni-involvement");
echo $shortDWName; // returns http://goo.gl/i002
echo "<br>";
// Test: Expand a URL
$longDWName = $googer->expand($shortDWName);
echo $longDWName; // returns https://davidwalsh.name
?>
<?php
// Declare the class
class GoogleUrlApi {
public $key=your key
// Constructor
function GoogleURLAPI($apiURL = 'https://www.googleapis.com/urlshortener/v1/url') {
// Keep the API Url
$this->apiURL = $apiURL.'?key='.$this->key;
}
// Shorten a URL
function shorten($url) {
// Send information along
$response = $this->send($url);
// Return the result
return isset($response['id']) ? $response['id'] : false;
}
// Expand a URL
function expand($url) {
// Send information along
$response = $this->send($url,false);
// Return the result
return isset($response['longUrl']) ? $response['longUrl'] : false;
}
// Send information to Google
function send($url,$shorten = true) {
// Create cURL
$ch = curl_init();
// If we're shortening a URL...
if($shorten) {
curl_setopt($ch,CURLOPT_URL,$this->apiURL);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode(array("longUrl"=>$url)));
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-Type: application/json"));
}
else {
curl_setopt($ch,CURLOPT_URL,$this->apiURL.'&shortUrl='.$url);
}
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
// Execute the post
$result = curl_exec($ch);
// Close the connection
curl_close($ch);
// Return the result
return json_decode($result,true);
}
}
// Create instance with key
$key = 'AIzaSyDUtkDIQ6uK7GCE68Lji_eQE9lPlo3Lk5Q';
$googer = new GoogleURLAPI();
// Test: Shorten a URL
$shortDWName = $googer->shorten("http://americanentrepreneurship.com/nj/entrepreneurs-featured/university-entrepreneurial-activities-draw-alumni-involvement");
echo $shortDWName; // returns http://goo.gl/i002
echo "<br>";
// Test: Expand a URL
$longDWName = $googer->expand($shortDWName);
echo $longDWName; // returns https://davidwalsh.name
?>
Subscribe to:
Posts
(
Atom
)