How to convert a number of spaces(if more than one) to single dash using PHP

134
July 25, 2021, at 6:30 PM

I can convert Space to dash.

$string = str_replace(' ', '-', $string);

But I want to convert a number of spaces; two, three, four, five, six or soon.. spaces into single dash

into -

Answer 1

Try this regex using preg_replace

$string = preg_replace( '!\s+!', '-', $string );
Answer 2
 preg_replace('/\/s+/g', '-', $str);
Answer 3

You can do this with preg_replace():

preg_replace('/[[:space:]]+/', '-', $string); 
Rent Charter Buses Company
READ ALSO
How to keep page content from scrolling without disabling scrolling?

How to keep page content from scrolling without disabling scrolling?

Alright so I'm really stumped by this oneHow can I lock the page's scroll position to a specific value? I can't use overflow: hidden; on the body, because that prevents scroll events from firing entirely

50
Game Maker Studio - Creating Login System Issue Stuck

Game Maker Studio - Creating Login System Issue Stuck

I've run into a major issue with one of my projects (which is currently in early access on Google Playstore) and I'm hoping for a bit of helpSo I'm using Microsoft PlayFab for my player accounts and made an option to use device ID's as a custom identifier

90
react-router-dom Link doesn't go to page content

react-router-dom Link doesn't go to page content

React router dom doesn't work when I use in NavLink (Reactstrap)

126
I don't really understand the purpose of brackets enclosing code

I don't really understand the purpose of brackets enclosing code

I'm just playing with integer arrays and find that the following won't compile with multiple errors:

113