How to pass image data using Ajax to server side in backend magento 2?

By default Image data are not post using ajax. only plain data will be passed as Ajax post. if we need to pass image data using ajax we need to call ajax as below way,
Call jquery using requirejs way in backend template file,
Let’s create a simple example of the form,

<form id="abcd" action="" enctype="multipart/form-data">
	<div class="control">
	    <input type='file' class='multi' name='attachment' id='attachment'/>
	</div>
	<button type="button" name="submitajax" id="submitajax"></button> 
</form>
<script>
    require(["jquery"],function($) {
        $("#submitajax").on('click', function() {
            var file = document.getElementById('attachment').files[0];
            var form;
            if (typeof file != 'undefined') {
                form = new FormData(document.getElementById('attachment'));
                form.append('form_key',window.FORM_KEY);
                form.append('attachment', file);
                form.append('id', getId);
            }
            $.ajax({
                url: "<?= $block->getUrl('myajax/custom/urlname'); ?>",
                type: 'post',
                mimeTypes: 'multipart/form-data',
                data: form,
                cache: false,
                contentType: false,  //this is must required
                processData: false, //this is must required
                beforeSend: function() {
                    $('body').trigger('processStart');
                },
                success: function (response) {
                    console.log(response);
                    $('body').trigger('processStop');
                }
            });
        });
    });
</script>

We need to Use You FormData() object of Javascript to store our image data and using above way we can just send image data to server side using ajax.

How to remove all special characters from a string with magento 2 as a best practice?

Magento Contains Match.php file they contain CONSTANT like SPECIAL_CHARACTERS and with that constant all the special character are available.
File path is Magento\Framework\Search\Adapter\Mysql\Query\Builder\Match.php

When we need to remove or replace special character with some other character you can use Magento default constant.

Match.php file with a constant declaration like as below,
const SPECIAL_CHARACTERS = ‘-+~/\\<>\'”:*$#@()!,.?`=%&^’;
When we need to modify a string with special character to some custom value or blank you can do as below way,

<?php
 $originalString = "Test1$23?45&789^1";

 $replaceSymbols =  str_split( \Magento\Framework\Search\Adapter\Mysql\Query\Builder\Match::SPECIAL_CHARACTERS, 1);
 $string = str_replace($replaceSymbols, ' ', $originalString));
 echo $string;

The result will be, Replace all special character with one space.
“Test1 23 45 789 1”

How to get customer data by customer id in magento 2?

Magento 2 Retrieve Customer Data Object by ID with the help of interface CustomerRepositoryInterface.

You can get customer information by just passing the customer id using below code snippet,

I have written function inside Block class,

<?php

namespace Rbj\Customer\Block;

class Customer extends \Magento\Framework\View\Element\Template
{
    /**
     * Constructor
     *
     * @param \Magento\Framework\View\Element\Template\Context  $context
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
        array $data = []
    ) {
        $this->customerRepository = $customerRepository;
        parent::__construct($context, $data);
    }

    /* Pass customer id $id*/
    public function getCustomer($id)
    {   
        return $this->customerRepository->getById($id);
    }

Call getCustomer($id) function in template file,

$customerId = 1; //pass dynamic customer id
$getCustomer = $block->getCustomer($customerId);
echo $customer->getFirstname();  // result customer first name
echo $customer->getEmail(); // result as customer email 
echo $customer->getLastname(); // customerr lastname

Get all customer data by the below way,

echo “<pre>”;print_r($customer->__toArray());

Array
(
    [website_id] => 1
    [email] => roni_cost@example.com
    [group_id] => 1
    [store_id] => 1
    [created_at] => 2017-11-10 13:09:03
    [updated_at] => 2017-11-23 12:48:29
    [disable_auto_group_change] => 0
    [created_in] => Default Store View
    [prefix] => 
    [firstname] => Veronica
    [middlename] => 
    [lastname] => Costello
    [suffix] => 
    [dob] => 1973-12-15
    [default_billing] => 1
    [default_shipping] => 1
    [taxvat] => 
    [confirmation] => 
    [gender] => 2
    [addresses] => Array
        (
            [0] => Array
                (
                    [firstname] => Veronica
                    [lastname] => Costello
                    [street] => Array
                        (
                            [0] => 6146 Honey Bluff Parkway
                        )

                    [city] => Calder
                    [country_id] => US
                    [region] => Array
                        (
                            [region] => Michigan
                            [region_code] => MI
                            [region_id] => 33
                        )

                    [region_id] => 33
                    [postcode] => 49628-7978
                    [telephone] => (555) 229-3326
                    [id] => 1
                    [customer_id] => 1
                    [default_billing] => 1
                    [default_shipping] => 1
                )

        )

    [id] => 1
    [extension_attributes] => Array
        (
            [is_subscribed] => 
        )

)