How to Remove Underscore from String in PHP?

Sep 15, 2022

.

Admin

How to Remove Underscore from String in PHP?

Hi Dev,

This example is focused on how to remove underscore from string in php?. you will learn remove underscore from string in php example. This article goes in detailed on php remove underscore from string. This tutorial will give you simple example of underscore remove from string in php. Alright, let’s dive into the steps.

There ara two ways to remove underscore from string in php. in the first example, we will use to str_replace() function and preg_replace() function use to remove underscore.Let's see both examples with output.

Example 1:


index.php

<?php

$str = "_Nice_nippets_.com_";

// Remove All Underscore from String

$result = str_replace("_", '', $str);

echo $result;

?>

Output:

Nicenippets.com

Example 2:

index.php

<?php

$str = "_My_webtuts_.com_";

// Remove All Underscore from String

$result = preg_replace("/_/i", '', $str);

echo $result;

?>

Output:

Mywebtuts.com

I hope it could help you...

#PHP