One line PHP code to remove characters except numbers and letters

I found this few lines of PHP script useful to sanitize user input, it removes characters except numbers and letters:

$clean_code = preg_replace('/[^\w]/', '', $string);

Or this one:

$clean_code = preg_replace('/[^a-zA-Z0-9]/', '', $string);

Original post is here: https://coderwall.com/p/bn47ka/remove-all-characters-except-letters-and-numbers-in-php

How to send POST request to another website from a PHP script

Let’s say we have a website like somewebsite.com and there is an input form, but we don’t want to submit that form from that website, instead we send POST request to that page from another website. Here is a handy PHP script to do so:

<?php

$url = 'https://somewebsite.com/';
$data = array('key1' => 'value1', 'key2' => 'vaue2');

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }

var_dump($result);

?>

Save Android Webview Content as PDF File

Hello everybody!

What you are seeing here is an Android app that shows a local html file inside a Web View.

Then on top of the web view there is a button to save web view contents as a pdf file.

Pdf file is saved localy after you tapped the button and you can open it using your favorite pdf viewer app in your phone.

How to make objects facing towards player in Unity

Some of elements in our game probably need to always face towards the player or player’s camera.

With this script we can make any object that this script attached on it, to look at a specific object, like player object or a camera.

How to resize QR Code image generated by phpqrcode library

In this tutorial I will show you how to modify the phpqrcode library to generate more bigger and larger QR Code image.

By default if we generate a QR Code of short text or string, we will get a small QR Code image approximately less than 100 pixels. But we can multiply by any number we like.

How to make an HTML5 Note Taking Program

HTML5 is amazing to me it and I have fun to do coding with JavaScript because it runs on web browser just like that, so we don’t have to install anything, its just running on web browser.

In this tutorial I will share with you how to create a web program, this time, a note taking program.

By watching this tutorial you will know how to store note data, how to retrieve it back and how to delete each one of notes we saved.

We don’t use any database to make this note taking program, we only use what so-called Local Storage on web browser, so our notes are saved on web browser.

Adding html file inside assets folder (Creating HTML5 Android WebApp)

Now we are ready to add our html file inside assets folder. Create a folder named assets and put your html file there. Then build the app. You will see your app is running displaying that html page.

This code is for html sample page used in this tutorial:

<!DOCTYPE html>
<html>
	<head>
		<title>LightWeight NoteTaker</title>
	</head>
	<body>
		<h1>Hello!</h1>
		<p>This is a LightWeight NoteTaker HTML5 Android app</p>
	</body>
</html>

Edit the MainActivity.java file (Creating HTML5 Android WebApp)

The next step is to edit MainActivity.java file. Copy and paste this code to your MainActivity, but remember keep your package name unchanged, my package name is “com.thirteenov.lightweightnotetaker” so you must your own package name as it is your unique app identifier.

package com.thirteenov.lightweightnotetaker;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    WebView browser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN
        );

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        browser = findViewById(R.id.webview);
        browser.setWebChromeClient(new WebChromeClient());
        browser.getSettings().setJavaScriptEnabled(true);
        browser.getSettings().setDomStorageEnabled(true);
        browser.getSettings().setAllowFileAccessFromFileURLs(true);
        browser.getSettings().setAllowUniversalAccessFromFileURLs(true);
        browser.getSettings().setMediaPlaybackRequiresUserGesture(false);

        browser.loadUrl("file:///android_asset/index.html");

        browser.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView viewx, String urlx) {
                if(!urlx.contains("http")){
                    viewx.loadUrl(urlx);
                    return false;
                }else{
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlx));
                    startActivity(intent);
                    return true;
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        WebView browser = findViewById(R.id.webview);
        if ((keyCode == KeyEvent.KEYCODE_BACK) &amp;&amp; browser.canGoBack()) {
            //if Back key pressed and webview can navigate to previous page
            browser.goBack();
            // go back to previous page
            return true;
        }
        else
        {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}

Adding the WebView to activity_main.xml in Android Studio (Creating HTML5 Android WebApp)

The next step is to add WebView to your layout file in Android Studio.

Delete everything in your layout before adding WebView. Then after you drag and drop the WebView, you need to make it full screen.

Make sure you set the constraint correctly and set the layout-width and layout-height to match constraint.

Give it an id: “webview”

Now we are done with layout. Next is to edit MainActivity.java file.