Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove parentheses and replace "white space" with "-" in php using preg_replace

Tags:

regex

php

This is What I Have - Huawei MatePad 11 (2021)

This is what I want - Huawei-MatePad-11-2021

This is what I have done so far. (only it replaces "white space" with "-".

echo preg_replace('/[[:space:]]+/', '-', $test);

I want to remove parentheses, and want to replace "white space" with "-" at once. How to do it?

like image 905
Roomal Jayaratne Avatar asked Feb 14 '26 05:02

Roomal Jayaratne


1 Answers

$text = 'Huawei MatePad 11 (2021)';
$text = preg_replace('/[(|)]/', '', preg_replace('/\s+/', '-', $text));

echo($text);
like image 171
marts Avatar answered Feb 16 '26 18:02

marts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!