Ad Code

PHP Functions/Classes

PHP Functions

function SafeURL($string){
    $string = strtolower($string); // Makes everything lowercase (just looks tidier).

$string = preg_replace('/[^a-z0-9]+/', '-', $string); // Replaces all non-alphanumeric characters with a hyphen.
    $string = preg_replace('/[-]{2,}/', '-', $string); // Replaces one or more occurrences of a hyphen, with a single one.
// $string = preg_replace('%', '-percentage', $string);
    $string = trim($string, '-'); // This ensures that our string doesn't start or end with a hyphen.
    return $string;
}


function create_url_slug($string){
   $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
   $string = preg_replace('/[^a-z0-9]+/', '-', $string); // Replaces all non-alphanumeric characters with a hyphen.
   return $slug;
}

echo create_url_slug('this is the example demo page');

// This will return 'this-is-the-example-demo-page'


// CURL POST
    function post_to_url($url, $data){
        $fields = '';
        foreach($data as $key => $value){
            $fields .= $key . '=' . $value . '&';
        }
       
        rtrim($fields, '&');
        $post = curl_init();
        curl_setopt($post, CURLOPT_URL, $url);
        curl_setopt($post, CURLOPT_POST, count($data));
        curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($post);
        curl_close($post);
    }
 
// To handle UPDATE or DELETE queries in PHP procedural style (mysqli) and get affected rows (and optionally return updated/deleted data using RETURNING in MySQL 8.0.19+)

$sql = "UPDATE users SET status = 'active' WHERE last_login > '2024-01-01'";

if(mysqli_query($conn, $sql)){
    echo "Rows affected: " . mysqli_affected_rows($conn);
}else{
    echo "Error: " . mysqli_error($conn);
}
 

 

Post a Comment

0 Comments