• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to split a string

#10
[eluser]jedd[/eluser]
To answer the question (subsequently edited, I'm happy to see) in your penultimate post -- I wanted to give you an algorithm so you could more easily identify if that was what you were after. I was having quite some trouble working out your goal here, and rather than produce code in response to your code, and dancing the night away in that fashion, I thought it easier to ... well, in any case, I'm glad that you got it going in three minutes.

[quote author="megabyte" date="1255416801"]now not to make you do any work, but if you wanted to give me a version that splits it in half by word count instead of character count......[/quote]

You make it sound like I'm the one that wants something done! Wink

Here's the result of my boredom.

You are likely going to be able to improve on this, especially if you loiter around the contrib-section of the php.net documentation pages for the functions I've used (and others). There's a bunch of very powerful manipulative functions that I never knew existed.

As you'll see in the output (below) you probably don't want a string split by word count - it is less likely to look normal than one split by string length and then broken at the next space. I accept that my demonstration string is heavily loaded to the left, but nonetheless, I still think strpos() is likely to look better, more often.

Finally, obviously methods 2 and 3 are functionally identical, but the difference you see on odd-numbered word counts can be removed with the judicious usage of a floor() function in there somewhere. I'll leave it to you to work out which one you prefer - I suspect the performance delta is trivial, though the third probably uses a touch more memory. Depends on the frequency of usage, and the sizes of your data, whether you'd notice any difference.


Code:
<?php
    $in_string = "Consider sentences commencifying with extraordinarily gratuitous grandiloquisms such as the type you might find on these forums.";

    echo "<b>". $in_string ."</b>";

    echo "<hr />";

    $result = split_by_size($in_string);
    echo $result['first'];
    echo "<br />";
    echo $result['second'];

    echo "<hr />";

    $result = split_by_word_count ($in_string);
    echo $result['first'];
    echo "<br />";
    echo $result['second'];

    echo "<hr />";

    $result = split_by_array ($in_string);
    echo $result['first'];
    echo "<br />";
    echo $result['second'];



    function split_by_size ($in_string)  {
        $result = array();

        $best_space = strpos ($in_string, " ", (strlen($in_string) / 2));
        $result['first'] = substr ($in_string, 0, $best_space);
        $result['second'] = ltrim (substr ($in_string, $best_space) );

        return $result;
        }


    function split_by_word_count ($in_string)  {
        $result = array();

        $middle_word = substr_count ($in_string, " ") / 2;

        $x = 0;
        $words_found = 0;
        $are_we_there_yet = FALSE;

        while ($are_we_there_yet == FALSE)  {
            if ($in_string[$x] == " ")
                $words_found++;

            if ($words_found >  $middle_word)
                $are_we_there_yet = TRUE;
            
            $x++;
            }

        $result['first']  = substr ($in_string, 0, $x);
        $result['second'] = substr ($in_string, $x);

        return $result;
        }


    function split_by_array ($in_string)  {
        $result = array ();

        $word_array = explode (" ", trim($in_string));
        $mid_by_word = sizeof ($word_array) / 2;

        $result['first']  = implode ( " ", array_slice ($word_array, 0, $mid_by_word) );
        $result['second'] = implode ( " ", array_slice ($word_array, $mid_by_word) );

        return $result;
        }


Output:
Code:
Consider sentences commencifying with extraordinarily gratuitous grandiloquisms such as the type you might find on these forums.

- - - - - - - - - - - - - - - - - - - - - - - - - - - -

Consider sentences commencifying with extraordinarily gratuitous
grandiloquisms such as the type you might find on these forums.

- - - - - - - - - - - - - - - - - - - - - - - - - - - -

Consider sentences commencifying with extraordinarily gratuitous grandiloquisms such as
the type you might find on these forums.

- - - - - - - - - - - - - - - - - - - - - - - - - - - -

Consider sentences commencifying with extraordinarily gratuitous grandiloquisms such
as the type you might find on these forums.


Messages In This Thread
how to split a string - by El Forum - 10-12-2009, 06:27 PM
how to split a string - by El Forum - 10-12-2009, 06:36 PM
how to split a string - by El Forum - 10-12-2009, 06:45 PM
how to split a string - by El Forum - 10-12-2009, 06:48 PM
how to split a string - by El Forum - 10-12-2009, 06:55 PM
how to split a string - by El Forum - 10-12-2009, 07:01 PM
how to split a string - by El Forum - 10-12-2009, 07:07 PM
how to split a string - by El Forum - 10-12-2009, 07:24 PM
how to split a string - by El Forum - 10-12-2009, 07:53 PM
how to split a string - by El Forum - 10-13-2009, 05:32 AM
how to split a string - by El Forum - 10-13-2009, 09:29 AM
how to split a string - by El Forum - 10-13-2009, 05:22 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.