I want to separate alpha-numeric (with a space) and non-alpha-numeric comma in a string.
I tried with this ...
$str = "This is !@ #$%^&"; preg_replace("/([a-z0-9_\s])([^a-z0-9_])/i", "$1, $2", $str);
But I got this result ...
This, is ,! @ # $% ^ &
How can I fix the pattarn search file to get this result?
It:! @ # $% ^ &
Thanks.
You should have canceled everything in the first group for the second, for example:
preg_replace("/([a-z0-9_\s])([^a-z0-9_\s])/i", "$1, $2", $str);
Otherwise, it will also be divided into spaces.
You will probably have to do this in several iterations. Try the following:
$preg = array( "/([\w\s]+)/i", "/([\W\s]+)/i" ): $replace = array( "\\1, ", "\\1 " ); $result = rtrim( preg_replace( $preg, $replace, $input ) ); // rtrim to get rid of any excess spaces
Source: https://habr.com/ru/post/1301888/More articles:C newbie cannot find syntax error - cWhat percentage of project hours should be the development of the developer (we carry) for maximum productivity - project-managementProtection against copying and files of Android applications - androidHow Safe Are Objective-C Categories? - objective-cThe target of parentheses in regular expressions - phpHow to avoid a conflict with the team leader? - project-management Go / Baduk / Weiqi Game Board - javascripthttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1301891/access-to-sql-db-in-multithread-server-app&usg=ALkJrhhWOApwcIAnqa9gIzqwM-gJtU253QHow does my visual addin studio detect compiler errors before building, much like "Delete and Sort Uses"? - c #List with different types - genericsAll Articles