Page 1 of 4 123 ... LastLast
Results 1 to 10 of 31

Thread: How do I extract a part of a string?

  1. #1
    Join Date
    Jan 2007
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow How do I extract a part of a string?

    Say I have the string:
    blasblahblooblezIneedtoextractthisboozblosblooblaheedsdfds
    How do I extract whatever is in between "blez" and "booz"?
    Last edited by jacksont123; 01-29-2007 at 01:13 AM.

  2. #2
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    That's a double question... double edged at that.

    Unless I am completely not understanding you, what is creating the string, where is it located, are you wanting to pull it out to display it on a page? Is it going to be saved off by itself? What do you want to pull it out with? Will it always be text, always the same length, etc etc.

    A little more on what you want to accomplish will help answer those questions
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  3. #3
    Join Date
    Jan 2007
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I found this script somewhere that does the same function but its in javascript.
    Code:
    		<script type="text/javascript">
    		<!-- // 
    		function CodeFix()
    		{
    			var strEmbedCode=new String (document.getElementById("txtEmbedCode").value);
    			var strStartMatch=new String("blez");
    			var strEndMatch=new String("blez");
    			
    			var intStart=0;
    			var intEnd=0;
    			
    			intStart=strEmbedCode.indexOf(strStartMatch, 0) + strStartMatch.length;
    			intEnd=strEmbedCode.indexOf(strEndMatch, intStart);
    			
    			if ((intStart != -1) && (intEnd !=-1))
    			
    			{
    				strEmbedCode=strEmbedCode.substring(intStart, intEnd);
    				strEmbedCode=unescape(strEmbedCode);
    				/*document.location.href=strEmbedCode;*/
    				window.location=strEmbedCode;
    				document.getElementById("downloadlink").innerHTML=strEmbedCode;
    				return true;
    			}
    			
    			else
    			{
    				alert("Input format incorrect!");
    			}
    		}
    		// -->
    		</script>

  4. #4
    Join Date
    Jan 2007
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by BLiZZaRD View Post
    That's a double question... double edged at that.

    Unless I am completely not understanding you, what is creating the string, where is it located, are you wanting to pull it out to display it on a page? Is it going to be saved off by itself? What do you want to pull it out with? Will it always be text, always the same length, etc etc.

    A little more on what you want to accomplish will help answer those questions
    The string will be displayed on the page, and the original may not be the same.
    I just need to extract what ever is in between "blez" and "booz" and display it on the page.

  5. #5
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Code:
    var query = "blez"
    var string = "blasblahblooblezIneedtoextractthisboozblosblooblaheedsdfds"
    alert(string.substring(string.indexOf(query),string.indexOf(query)+query.length))
    do the same for booz or whatever
    - Mike

  6. #6
    Join Date
    Jan 2007
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Is that for php or javascript?
    and will that extract and display whatever is in booz and blez?

    (sorry, i would have made better words but i couldn't think of anything at the time)

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    EDIT: Apparently the original post was edited, changing the question. The answer relating to the new question is on the next page.
    However, this applies well (and fills in some details missing in the next post) if you want to find "blez" AND "blez", or two of the same markers, not different markers.



    Several methods...

    1.
    PHP Code:
    list($extra,$YOURVAR,$extra2) = explode('blez',$string,3); 
    $YOURVAR now holds that value.

    2.
    PHP Code:
    $YOURVAR substr($string,strpos($string,'blez')+strlen('blez'),(strpos($string,'blez',strpos($string,'blez')+strlen($marker))-(strpos($string,'blez')+strlen('blez')))); 
    Again, $YOURVAR now holds the value.


    I think the substr method would theoretically best less server load or some such, but, hey, I think explode is a little bit friendlier



    More info:
    1.
    list() acts like an array, but with specific variables. You could just use $myvar = explode(...) instead, and $myvar[1] (since arrays start at zero, 1 is the second value) would be what you want.
    explode() splits a string into chunks (in an array) at a marker. The third parameter is limiting the number of outputs to three, so the next blez, if it were to occur would not split again, causing confusion.

    2. substr(STRING, INT START, INT LENGTH) (aka sub-string)
    So... you set which string to use, what point to start and how long it should be. This can all be determined using strpos() (aka string-position, within a string):
    strpos(STRING, INT START, INT OFFSET)
    So... which string, where to start, then, if added, how far into the string to start looking. We use this here because you need two instances of the same string, so you need to be sure not to just find the first occurance twice.
    Note: strlen($var) just returns to length of $var, helping with the offset and such.



    For a better understanding of method two, here are two ways to look at it:
    First, make it generic. Let's use a variable instead of your randomly chosen marker:
    $marker = 'blez';
    substr($string,strpos($string,$marker)+strlen($marker),(strpos($string,$marker,strpos($string,$marker)+strlen($marker))-(strpos($string,$marker)+strlen($marker))));

    Now, to further explain, we can seperate that into lots of parts:
    PHP Code:
    $marker 'blez';
    //Here's what we're starting with--
    substr($string,strpos($string,$marker)+strlen($marker),(strpos($string,$marker,strpos($string,$marker)+1)-(strpos($string,$marker)+strlen($marker))));
    //To begin...
    $len strlen($marker); //$len is now length of marker
    $pos1 strpos($string,$marker); // find pos1 (first position) of marker in string
    //already, it looks a lot simpler:
    substr($string,$pos1+$len,(strpos($string,$marker,$pos1+$len)-($pos1+$len)));
    //Now, to continue:
    substr($string,$pos1+$len,(strpos($string,$marker,$pos1+$len)-($pos1+$len)));
    //We don't want to include the marker, so let's add it's length to the starting point--
    $start $len $pos1;
    //Now, even more simple:
    substr($string,$start,(strpos($string,$marker,$start)-$start));
    //Let's find the second occurance of $marker:
    $pos2 strpos($string,$marker,$start);
    //find position of $m in $s AFTER starting point, $start
    //So...
    substr($string,$start,($pos2-$start));
    //Let's simplify the end...
    //Since we want what is BETWEEN $start and $pos2, let's just subtract:
    $length = ($pos2-$start);
    //Now, looks a lot nicer:
    substr($string,$start,$length);

    //So, here's all of that without the 'work':
    $len strlen($marker);
    $pos1 strpos($string,$marker);
    $start $len $pos1;
    $pos2 strpos($string,$marker,$start);
    $length = ($pos2-$start);
    $MYVAR substr($string,$start,$length); 

    EDIT: Note that in the time it took me to write all of the comments and such, I now find 5 posts have been added... I *was* going to be the first reply
    Last edited by djr33; 01-29-2007 at 01:54 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Sorry, my internet disconnected me! I couldn't finish my post.
    And I forgot this was in the php section.. whoa I'm sleep deprived...
    This is a javascript way to do it:
    Code:
    <script type="text/javascript">
    function getstr(query1,query2,string) {
    document.write(string.substring(string.indexOf(query1)+query1.length,string.indexOf(query2)));
    }
    var string1 = "blasblahblooblezIneedtoextractthisboozblosblooblaheedsdfds"
    var myquery1 = "blez"
    var myquery2 = "booz"
    getstr(myquery1,myquery2,string1)
    </script>
    - Mike

  9. #9
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    indexOf, and strpos are almost identical between JavaScript and PHP. Either way works.
    - Mike

  10. #10
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    thetestingsite: I changed it to document.write, I got cut off and couldn't edit my post.
    - Mike

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •