PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

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

 November 20, 2022     php, preg-match, preg-replace, replace, string     No comments   

Issue

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 -


Solution

Try this regex using preg_replace

$string = preg_replace( '!\s+!', '-', $string );


Answered By - Amit
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing