codeigniter parse and replace bbcode - codeigniter

I use BBCode Helper.
helper code:
function parse_bbcode($str = ''){
$find = array(
"'\[v\](.*?)\[/v\]'is"
);
$replace = array(
'<video>\1</video>'
);
return preg_replace($find, $replace, $str);
}
controller:
$this->load->helper('bbcode');
$data['news'] = $this->news_model->get_news($config['per_page'], $this->uri->segment(3, 1));
foreach ($data['news'] as $key=>$val)
{
parse_bbcode($data['news'][$key]['description']);
}
for example i want to replace [v]vid[/v] to vid.
replacement does not work (nothing happens). what I did wrong?

Your regex is wrong.
This works:
$video = "[v]testvid.swf[/v]";
$re = '/\[v](.+)\[\/v\]/i';
$replace = '<video>\1</video>';
echo htmlspecialchars(preg_replace($re, $replace, $video));
Outputs:
<video>testvid.swf</video>
http://regexpal.com/ is a good resource for working out regex issues.

Related

Unable to save base 64 Image in Laravel 8

I am trying to save base64 image that is coming from the ajax post (blade file). Below is the code that I am using to save the data but it is giving 500 error.
public function add_ref_images_first(Request $request){
$fileName = "";
$end_url = "";
$count = 0;
$folder_name = 'PUBP' . time();
foreach ($request->images as $data){
$image_64 = $data['src']; //your base64 encoded data
$extension = explode('/', explode(':', substr($image_64, 0, strpos($image_64, ';')))[1])[1]; // .jpg .png .pdf
$replace = substr($image_64, 0, strpos($image_64, ',')+1);
//
// // find substring fro replace here eg: data:image/png;base64,
//
$image = str_replace($replace, '', $image_64);
$image = str_replace(' ', '+', $image);
$ref_image_id = 'PUBR'.time().$count++.'.'.$extension;
$fileName = base64_decode($image)->storeAs($folder_name, $ref_image_id , ['disk' => 'my_uploaded_files']);
if($imageName){
$end_url = $end_url.$imageName.',';
}
}
return response()->json(['url' => $end_url, 'id' => '1']);
}
Is there issue with the code?
instead of the line
$fileName = base64_decode($image)->storeAs($folder_name, $ref_image_id , ['disk'
=> 'my_uploaded_files']);
you can do :
use Illuminate\Support\Facades\Storage;
Storage::put($ref_image_id, base64_decode($image), 'local');
because basically you were trying storeAs on a string value, storeAs works on $request->file('nameFromForm')
reference: https://laravel.com/docs/8.x/filesystem
https://laravel.com/docs/8.x/filesystem#specifying-a-file-name

Code sniffer for for converting snake_case to camelCase

I need a code sniffer for converting all the variables in all of my PHP file from snake_case to camelCase.
I don't need the functions which do it in php on a string, I want to convert the variables in my PHP files to camelCase.
I appreciate you in advanced.
I found a way to do it.
I integrate the code for cakePHP (There is the file with ctp extension and the variable passed to view by set function).
Save the below code as tocamelcase.php
php tocamelcase.php the/path/of/your/app
file content:
<?php
function snakeToCamel($val) {
preg_match('#^_*#', $val, $underscores);
$underscores = current($underscores);
$camel = str_replace('||||', '', ucwords(str_replace('_', '||||', $val), '||||'));
$camel = strtolower(substr($camel, 0, 1)).substr($camel, 1);
return $underscores.$camel;
}
function convert($str) {
global $j;
$name = '/(\$[a-zA-Z0-9]+_[a-zA-Z0-9_]+)|'.
'(->[a-zA-Z0-9]+_[a-zA-Z0-9_]+)|'.
'(::[a-zA-Z0-9]+_[a-zA-Z0-9_]+)|'.
'(\sfunction\s+[a-zA-Z0-9]+_[a-zA-Z0-9_]+)|'.
'(\$this->set\(\'[a-zA-Z0-9]+_[a-zA-Z0-9_]+\'\,)|'.
'(\$this->set\(\"[a-zA-Z0-9]+_[a-zA-Z0-9_]+\"\,)'.
'/i';
$str = preg_replace_callback($name, function($matches){
return snakeToCamel($matches[0]);
},$str);
return $str;
}
$path = $argv[1];
$Iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$i=1;
foreach($Iterator as $file){
if(substr($file,-4) !== '.php' && substr($file,-4) !== '.ctp')
continue;
echo($i.": ".$file."\n");
$i++;
$out = convert(file_get_contents($file));
file_put_contents($file, $out);
}

joomla - router change url when getting the name of product

I have build my own component in joomla and client wants now a friendly urls f.e
website.com/someplace/{product-id}-{product-name}. So i Build my own router like this.
function componentBuildRoute(&$query)
{
$segments = [];
if (isset($query['view'])) {
$segments[] = "szkolenie";
unset($query['view']);
}
if (isset($query['product_id'])) {
$productName = JFilterOutput::stringURLSafe(strtolower(getProductName($query['product_id'])));
$newName = $query['product_id'] . '-' . $productName;
$segments[] = $newName;
unset($query['product_id']);
}
return $segments;
}
and parse route function
function componentParseRoute($segments)
{
$app = JFactory::getApplication();
$menu = $app->getMenu();
$item =& $menu->getActive();
$count = count($segments);
switch ($item->query['view']) {
case 'catalogue' : {
$view = 'training';
$id = $segments[1];
}
break;
}
$data = [
'view' => $view,
'product_id' => $id
];
return $data;
}
While on the end of buildroute function segments are ok I have exactly what I want that on the beginning of parse route I have something like
website.com/szkolenie/1-krakow <-- I dont know wtf is this krakow( I know it is city i Poland) but still where is it get from ? The getProductName function implementation is
function getProductName($productId)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('#__component_training.id as id, #__component_product' . name)
->from($db->quoteName('#__component_training'))
->where('#__s4edu_product.product_id = ' . $productId)
->leftJoin('#__component_product ON
#__component_training.product_id=#__component_product.product_id');
$training = $db->loadObject();
return trim($training->name);
}
So taking all this into consideration I think that something is happening between the buildRoute and parseRoute, something what filters the $segment[1] variable, but how to disable that and why is it happening ?
P.S
Please do not send me to https://docs.joomla.org/Joomla_Routes_%26_SEF
I already know all the tutorials on joomla website which contains anything with sef.
P.S.S
It is built on joomla 3.7.0
You do not have a product named "krakow" ?
If not you can try to remove the $productName from the build function, just to check if this "krakow" is added automaticaly or it's from the getProductName() function.
Also i noticed that you have an error i guess in the function getProductName()
->where('#__s4edu_product.product_id = ' . $productId)
It's should be
->where('#__component_product.product_id = ' . $productId)

get the google fonts list in page

i'm using laravel now i want to list all the google web fonts in my page using google font api
public function index(){
function get_google_fonts() {
$url = "https://www.googleapis.com/webfonts/v1/webfonts?key=!";
$result = json_response( $url );
$font_list = array();
foreach ( $result->items as $font ) {
echo $font;
}
return $font_list;
}
return view('website_settings');
}
i think it's not working, can anyone help me please!
because $font is an object you have to traverse.. try something like that
$url = "https://www.googleapis.com/webfonts/v1/webfonts?key=!";
$result = json_decode(file_get_contents( $url ));
$font_list = "";
foreach ( $result->items as $font )
{
$font_list[] = [
'font_name' => $font->family,
'category' => $font->category,
'variants' => implode(', ', $font->variants),
// subsets
// version
// files
];
}
return $font_list;

In ZF2 how to Escape HTML in Custom View Helpers

Here, I see someone suggesting to extend the Zend\View\Helper\Escaper\AbstractHelper;, resulting in something like this:
namespace Photo\View\Helper;
use Zend\View\Helper\Escaper\AbstractHelper;
class Imghelper extends AbstractHelper
{
protected $_baseUrl = null;
protected $_exists = array();
public function __construct ()
{
$url = PUBLIC_PATH;
$root = '/' . trim($url, '/');
if ($root == "/")
{$root = "";}
$this->_baseUrl = $root . "/";
}
public function escape($value)
{
return $this->getEscaper()->escapeHtml($value);
}
public function __invoke($path, $params = array())
{
$plist = array();
$str = null;
$imgpath = $this->_baseUrl . ltrim($path);
if (!isset($this->_exists[$path]))
{$this->_exists[$path] = file_exists(realpath($imgpath));}
if (!isset($params['alt']))
{$params['alt'] = '';}
foreach ($params as $param => $value)
{$plist[] = $param . '="' . $this->escape($value) . '"';}
$str = " ".join(" ", $plist);
$p = ($this->_exists[$path]) ? $this->_baseUrl.ltrim($path,'/') : null;
if (isset($p))
{return '<img src="'.$p.'" '.$str.' />';}
}
}
However, when I try it in my codes, I get an Error
Strict Standards: Declaration of Application\View\Helper\Imghelper::__invoke() should be compatible with Zend\View\Helper\Escaper\AbstractHelper::__invoke($value, $recurse = 0) in ...
Is it ok to error the error message? Or is there any better ways to do this? Thanks!
I wouldn't recommend extending the Abstract Escape helper since you aren't writing an escape helper. Stick to your original plan and just extend Zend\View\Helper\AbstractHelper. You can then access the escape helpers via. $this->view->escapeHtml(), $this->view->escapeJs() and so on.

Resources