How do I get the middle of a string?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kingdon
    New Member
    • Aug 2006
    • 5

    How do I get the middle of a string?

    I have a question. If I have a string like this...

    $a = "I don't need this @This is the middle I need # This is the end I don't need";

    Does anyone know the command that would let me get "This is the middle I need" out of this string and put it into $b?

    Any help would be appreciated.
    Thanks
    Don
  • blade_in_exile
    New Member
    • Aug 2006
    • 12

    #2
    why would you need to pull the centre out of this line, if you set up your html/php code correctly it could be on its own therefore no need to pull it out of the $a

    Comment

    • kingdon
      New Member
      • Aug 2006
      • 5

      #3
      I have a database of full names such as "Michael Ian Black". I want to extract "Ian" from this string. Bascially I want to know the commands to strip "Michael " from this string by searching for the first space. Then have something like this.

      $a = "Michael Ian Black";
      $b = "Ian Black";

      Then I want to search for the first space again and strip everything after it.

      $b = "Ian Black";
      $c = "Ian";

      Does anyone know the commands for this or can write a simple code to show me how to do this?

      Thanks
      Don

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        If you always know what your separators are, the following function will do.

        [PHP]
        <?php
        $a = "I don't need this @This is the middle I need # This is the end I don't need";
        if ($b = extractit($a, '@', '#'))
        echo $b;
        else
        echo 'Not found';

        // the above will return: This is the middle I need
        /////////////////////////////////////////////////////////////
        // specify: string, start separator, close separator
        function extractit($stri ng, $s, $e) {
        if ($start=strpos( $string, $s)) {
        if ($end=strpos(su bstr($string,$s tart+1), $e)) {
        return substr($string, $start+1, $end-1);
        }
        else {
        return false;
        }
        }
        return false;
        }
        ?>
        [/PHP]

        Ronald :cool:

        Comment

        • kingdon
          New Member
          • Aug 2006
          • 5

          #5
          Ronald,

          I get an error when I use that command. Any other ideas or do you know why I would get that error?

          Fatal error: Call to undefined function: extractit()

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            You must have copied/pasted something wrong from the forum entry. It is a normal PHP function. Reworked outside a function it is as follows:
            [PHP]<?php
            $a = "I don't need this @This is the middle I need # This is the end I don't need";
            if ($start=strpos( $a, '@')) {
            if ($end=strpos(su bstr($a,$start+ 1), '#')) {
            $b = substr($a, $start+1, $end-1);
            echo $b;
            }
            else {
            echo 'end-separator not found';
            }
            }
            else
            echo 'start-separator not found';
            ?>[/PHP]
            Ronald :cool:

            Comment

            • kingdon
              New Member
              • Aug 2006
              • 5

              #7
              Ronald,

              You are right. I must of copied something wrong. I just tried it and it worked. Thank you very much. You saved me hours of trying to figure this out.

              Dean

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                A last word on this: I asume you use the @ and # just to be able to distinguish between the first and last separator. You could also use just one standard separator, like @.

                That way you can use the PHP explode() function. And then the code would be alot easier, such as:

                [PHP]<?php
                $a = "I don't need this @This is the middle I need.@ This is the end I don't need";
                $parts = explode("@", $a)
                echo $parts[0] // results in: I don't need this
                echo $parts[1] // results in: This is the middle I need.
                echo $parts[2] // results in: This is the end I don't need
                ?>[/PHP]
                Keep cool!

                Ronald :cool:

                Comment

                • kingdon
                  New Member
                  • Aug 2006
                  • 5

                  #9
                  Actually I needed 2 separate separators in this case. But I may be able to use your other example in the future.

                  Thanks again
                  Dean

                  Comment

                  Working...