Replace blank space with Hyphen/Dash inside string in PHP

How to remove blank white space between string words and overlap them with hyphen using PHP.

In this tutorial we are simply creating replacing the blank space with hyphen( – ). This is kind of functionality mostly used in dynamic php websites where sometimes user upload images with space and mostly used to create permalinks URL structure. So here is the complete step by step tutorial for Replace blank space with Hyphen/Dash inside string in PHP.

android-project-download-code-button

How to Replace blank space with Hyphen/Dash inside string in PHP.

Code for replace-space-with-dash.php file.

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>

<?php

$demo_var = "Android Examples";

$demo_2 = str_replace(' ', '-', $demo_var);

echo "Without replace space = " . $demo_var . '</br>';

echo "With space replace = " . $demo_2;
?>

</body>
</html>

Screenshot:

Replace blank space with Hyphen/Dash inside string in PHP

Click here to download Replace blank space with Hyphen/Dash inside string in PHP project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *