Jump to content

How can i divide this[String]?


sangoku

Recommended Posts

hy guys i have a problem with this functino somone knows how to solve it      >.<

 

here  is the code

    function humanize($input){

        $preg_replace = array(
        '/([^A-Z])([A-Z][a-z]+)/U' => '$1 $2',
        '/([^a-z])([a-z]+)/Ui'     => ' $1$2',
        '/([a-z]+)([^a-z])/Ui'     => '$1$2 ',
        );
        $input = preg_replace(
        array_keys( $preg_replace ),
        $preg_replace,
        $input
        );
        $input = preg_split( '/\s+/', $input, -1, PREG_SPLIT_NO_EMPTY );
        $input = implode( ' ', $input );
        return $input;
    }
    
    $string = 'test Test TesT test[test] test(test) test[test test] test[test]test';
    $result = humanize($string);
    //it outputs "test    Test    TesT   test  [test]   test  (test)   test  [test   test]   test  [test  ]test"
    //but it should be like this
    //"test    Test    TesT   test  [test]   test  (test)   test  [test   test]   test  [test] test"



 

 

As told in code

 

    //it outputs "test    Test    TesT  test  [test]  test  (test)  test  [test  test]  test  [test  ]test"

    //but it should be like this

  //"test    Test    TesT  test  [test]  test  (test)  test  [test  test]  test  [test] test"

 

 

Somone knows how to solve this?

 

the input string is 'test Test TesT test[test] test(test) test[test test] test[test]test'

 

 

Link to comment
https://forums.phpfreaks.com/topic/198031-how-can-i-divide-thisstring/
Share on other sites

Taking a stab since you didn't really state your requirements...this passes your test at least.

 

<?php
function humanize($input){
    return preg_replace('/
        (?<=[\]})]) # Closing char to the left
        (?=[A-Za-z]) # Letter to the right
    /x', ' ', # Insert a space
        preg_replace('/
            (?<=[A-Za-z]) # Letter to the left
            (?=[[({]) # Opening char to the right
        /x', ' ', $input) # Insert a space
    );
}


$string = 'test Test TesT test[test] test(test) test[test test] test[test]test';
echo humanize($string);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.