Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts

    String Handling (strstr'ing)

    Hi Guys,

    I've got a whole bunch of text in an array, called $lines, for example. Each array element represents a line.

    I use foreach($lines as $line), I then use strstr($line, "TEXTTOSEARCH")

    It all works absolutely fine, except when "TEXTTOSEARCH" appears twice in the array.

    How can I make it so that instead of the second occurence overwriting the previous occurences stuff, it just gets appended?

    I don't know how to seperate them out. Perhaps some way of counting the array elements would be good, because identical text only ever occurs in sequential lines. ie if($lines[arraycounter + 1] == "TEXTTOSEARCH") somehow append.

    Forgive me, i'm still learning concepts, being very new to programming.

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    I think you're going to have to give some more detail on this one.

    What is your intent of using strstr()? Are you just trying to do a search and replace?

    What do you mean by:

    How can I make it so that instead of the second occurence overwriting the previous occurences stuff, it just gets appended?
    Perhaps post what you have currently.

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by aedrin View Post
    Perhaps post what you have currently.
    Ok!

    Here's what I have so far...

    PHP Code:
    <?php

    $dir 
    "/var/www/blues/";

    $dtitle 'DTITLE=';
    $dgenre 'DGENRE=';
    $dyear 'DYEAR=';
    $ttitle 'TTITLE';


    $tstring "Track frame offsets:";

    $record FALSE;
    //$duration = array();

    function trimAndCase($input)
    {
        
    $input trim($input);
        
    $input ucwords(strtolower($input));
        return 
    $input;
    }
    $dir opendir($dir);
    while ((
    $file readdir($dir)) !== false)
    {
        if(!
    is_dir($file))
        {
            
    $lines file("/var/www/blues/".$file); //the readdir function changes the variable to "Resource Handle 3, so I had to specify the path manually again.
                
    foreach($lines as $line)
                {
                    
    trim($line);
                    if(
    strstr($line$dtitle))
                    {
                        
    $line substr($linestrlen($dtitle));
                        
    $line trim($line);
                        
    // if line ends with a /, remove the /.
                        
    if(substr($line, -1) == "/")
                        {
                        echo 
    "ends with a slash ";
                            
    $line substr($line0, -1);
                            
    $line trim($line);
                        }
                        
    // if the string contains a clash, use this to explode it.
                        
    if(strstr($line"/"))
                        {
                            
    $elements explode("/"$line);
                            
    //$artist is the first element before the /
                            
    $artist trimAndCase($elements[0]);
                            if(
    count($elements) > 2)
                            {
                                
    $release trimAndCase($elements[1] . "/" $elements[2]);
                                echo 
    "More than two \"/\" " $release ;
                            }
                            else
                            {
                                
    $release trimAndCase($elements[1]);
                            }

                        }
                        else {
                        echo 
    "Only one DTITLE element";
                            
    $artist $line;
                            
    $release $line;
                        }
                        if((
    $artist == "V/A") || ($artist == "Various Artists")) {
                            
    $artist "Various";
                        }
                    }
                    
                }
    echo 
    '<a href="' "/blues/" $file '">' $file '</a>  ' "Artist: " $artist " Release: " $release "<br>";
            }
        }

      
    closedir($dir);
    ?>

    You can see the results at http://87.74.42.101/test.php

    Thanks for your help.

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Now you just have to indicate which specific parts are troubling you. Or what exactly you're trying to accomplish.

    I can see you're parsing a directory to produce a playlist. But the playlist looks fine to me.

  • #5
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    Apologies... i'll be more specific in future..

    Ok,

    go to http://87.74.42.101/test.php and search for 2212b614


    Clicking on it will show the file has two DTITLE entries, which is the cause of the problem.

    Thanks...

    P.S. I edited it just now with correct details.
    Last edited by samuurai; 11-20-2007 at 04:50 PM.

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Without looking at your code, the following comes to mind:

    PHP Code:
    function startsWith($subject$startsWith) {
        return 
    substr($subject0strlen($startsWith)) == $startsWith;
    }

    $titleKeyword 'DTITLE';
    $artist '';

    foreach (
    $lines as $line) {
        if (
    startsWith($line$titleKeyword)) {
            
    $artist .= substr($linestrlen($titleKeyword));
        }
        
    // check for other information
    }

    echo 
    'Artist = ' $artist

  • Users who have thanked aedrin for this post:

    samuurai (11-20-2007)

  • #7
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    Wow, I didn't expect the actual code.. your approach is genius.

    I can use this approach to make the rest of my script neater now that I know it... thanks very much!

    Beren

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    No problem.

    Sometimes it is easier to take a different road after hitting a roadblock.

  • #9
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    Nice metaphor

  • #10
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by aedrin View Post
    Without looking at your code, the following comes to mind:

    PHP Code:
    function startsWith($subject$startsWith) {
        return 
    substr($subject0strlen($startsWith)) == $startsWith;
    }

    $titleKeyword 'DTITLE';
    $artist '';

    foreach (
    $lines as $line) {
        if (
    startsWith($line$titleKeyword)) {
            
    $artist .= substr($linestrlen($titleKeyword));
        }
        
    // check for other information
    }

    echo 
    'Artist = ' $artist
    You can simplify this:
    PHP Code:
    $titleKeyword 'DTITLE';
    $artist '';

    foreach (
    $lines as $line) {
        if (
    strpos($line$titleKeyword) === 0) {
            
    $artist .= substr($linestrlen($titleKeyword));
        }
        
    // check for other information
    }

    echo 
    'Artist = ' $artist
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • Users who have thanked marek_mar for this post:

    samuurai (11-22-2007)

  • #11
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Not sure if it's really simplified.

    Using a function makes it easier to read, and reusable. Not to mention that comparing 2 short strings is in most cases more efficient/faster than searching through the entire string.

  • #12
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by aedrin View Post
    PHP Code:
    return substr($subject0strlen($startsWith)) == $startsWith
    I'm having trouble understanding this line...

    I understand that you're removing the part of the line that contains $startsWith by counting the characters in it and lopping it off the start, but I don't get the == $startsWith; at the end. - Is that a shorthand way of making a calculation, then setting it to a variable and returning it, all in one line?

  • #13
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by marek_mar View Post
    You can simplify this:
    Thanks a lot.. it's good to know more than one way of doing it. - I was about to say "both ways", but I'm gathering with programming that there are usually hundreds of ways of achieving the same thing.

  • #14
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Quote Originally Posted by samuurai View Post
    I'm having trouble understanding this line...

    I understand that you're removing the part of the line that contains $startsWith by counting the characters in it and lopping it off the start, but I don't get the == $startsWith; at the end. - Is that a shorthand way of making a calculation, then setting it to a variable and returning it, all in one line?
    Actually, it does something different.

    Substr() returns a subset of a string, determined by a start and an (optional) length. So I'm taking the first X characters (X is the length of the keyword), and compare it to the actual keyword.

    You are correct about the latter part though. Everything after the return is considered an expression which is simplified (evaluated) until it is no longer possible to further simplify. In this case, that happens to be a simple boolean (true or false) which is the result of the comparison.

  • #15
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    I'm struggling with some of the terms, "expression" and "simplify", but I'm fairly sure I've got the meaning.

    Regardless, i've got my script working well using the concepts from your example. Thanks again!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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