SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot kozata-xakep's Avatar
    Join Date
    Feb 2008
    Location
    Varna, Bulgaria
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    strstr() question

    Hi everyone !

    I'm trying to use the strtstr() function to get the first line of a paragraph. Unfortunately, I'm getting everything after the needle.

    I'm using an example from php.net

    PHP Code:
    $email  'name@example.com';
    $user strstr($email'@'true); // From PHP 5.3.0
    echo $user// returns name
    ?> 
    This is mine:

    PHP Code:
    <?php
        
    include_once('../db_fns.php');
      
          
    $handle db_connect();
        
    $pages_sql 'select * from pages order by code';

          
    $pages_result $handle->query($pages_sql);
              
          while (
    $pages $pages_result->fetch_assoc()) 
          {
            
    $story_sql "select * from stories
                          where page = '
    {$pages['code']}'
                          and published is not null
                          order by published desc"
    ;
       
            
    $story_result $handle->query($story_sql);
            
            if (
    $story_result->num_rows
            {
              
    $story $story_result->fetch_assoc();
              
    $shortstory strstr($story["story_text"],'.'true);
              echo 
    $shortstory;
            }
           }    
    ?>
    This code works if I remove the TRUE from strstr(). Else I get Warning: Wrong parameter count for strstr()

    If you have any ideas how to fix that please post a comment.

    Thank you in advance !

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    What version of PHP are you running?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Zealot kozata-xakep's Avatar
    Join Date
    Feb 2008
    Location
    Varna, Bulgaria
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My hosting company PHP version is 5.2.5

    P.S. spikeZ, your avatar is cool as always. Everything I see it makes laught

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Location
    New York
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Third parameter was added in PHP ver. 6.0.0 according to PHP Manual...
    www.forkaya.com - Web Development, PHP Scripting

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Location
    New York
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to get the same result as setting third parameter to 'True', you can replace:
    Code PHP:
    $shortstory = strstr($story["story_text"],'.', true);
    with the following code:
    Code PHP:
    $shortstory = false;
    $pos = strpos($story["story_text"],'.');
    if ($pos) {
    	$shortstory = substr($story["story_text"],0,$pos);
    }
    www.forkaya.com - Web Development, PHP Scripting

  6. #6
    SitePoint Zealot kozata-xakep's Avatar
    Join Date
    Feb 2008
    Location
    Varna, Bulgaria
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well it doesn't work, nothing happens, not even an error.

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Location
    New York
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you echo $story["story_text"] to see if '.' is there? Because if it is not, the code is working fine... meaning that the following line:
    Code PHP:
              echo $shortstory;
    have nothing to print because $shortstory is false.
    www.forkaya.com - Web Development, PHP Scripting

  8. #8
    SitePoint Zealot kozata-xakep's Avatar
    Join Date
    Feb 2008
    Location
    Varna, Bulgaria
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I can echo the $story['story_text']. It was developed without the strstr() and it works.

    The " . " is where it should be.

  9. #9
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Location
    New York
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure if I follow... So my code is working for you?
    www.forkaya.com - Web Development, PHP Scripting

  10. #10
    SitePoint Zealot kozata-xakep's Avatar
    Join Date
    Feb 2008
    Location
    Varna, Bulgaria
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope, your code is not working. If I was getting an error with "true" inside the strstr(), with your code nothing happens. All the php code (yours) disappear.

  11. #11
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Location
    New York
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure why not. The following code:

    Code PHP:
    $story = 'abca123.4efg';
    echo('<b>story:</b> '.$story.'</br>');
    $shortstory = false;
    $pos = strpos($story,'.');
    if ($pos) {
    	$shortstory = substr($story,0,$pos);
    }
    echo ('<b>short story:</b> '.$shortstory);

    outputs:

    story: abca123.4efg
    short story: abca123
    so it should work for you...
    www.forkaya.com - Web Development, PHP Scripting

  12. #12
    SitePoint Zealot kozata-xakep's Avatar
    Join Date
    Feb 2008
    Location
    Varna, Bulgaria
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I forgot to echo at the end the $shortstory

    Thank you for helping me out. Well done.

    Happy New Year meeting !


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
  •