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
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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.