SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get text within separators in a string

    Hi guys

    I need to extract text within two separators in a string like below:
    PHP Code:
    {available_now}
    this text I need to extract
    {/available_now
    How can I do that?

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,833
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by Zaggs View Post
    Hi guys

    I need to extract text within two separators in a string like below:
    PHP Code:
    {available_now}
    this text I need to extract
    {/available_now
    How can I do that?
    Try this:
    PHP Code:

      $a
    ="{available_now} 
                this text I need to extract 
             {/available_now}"
    ;

       echo 
    $a .'<br />'
       
       
    $b substr($a15); // remove "{available_now}"

       # both remove trailing "{/available_now}"
       
    if(0)
      {
          
    $b str_replace("{/available_now}"''$b);
       }
       else
       {
            
    $b substr($b0, -16);
       }

       echo 
    $b .'<br />'
    Last edited by John_Betong; Nov 7, 2012 at 08:51. Reason: Whooops - misread request, added substr(...)
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by John_Betong View Post
    Try this:
    PHP Code:

      $a
    ="{available_now} 
                this text I need to extract 
             {/available_now}"
    ;

       echo 
    $a .'<br />'
       
       
    $b substr($a015) ."{/available_now}";

       echo 
    $b .'<br />'
    Sorry I forgot to mention, I need to do this dynamically (i.e. the separators and text may change)

  4. #4
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,833
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by Zaggs View Post
    Sorry I forgot to mention, I need to do this dynamically (i.e. the separators and text may change)
    Here we go:
    PHP Code:

    #======================
    #
    # extract text between separators
    # usage:  _extract($text_with_seperators) 
    # return:  text between separators

    #======================
    function _extract($a)
    {
      
    // find start
      
    $x strpos($a"}");
      
    $b substr($a, ++$x); // remove "{any-length-text-goes-here}"

      
    $y strpos($b"{");
      
    $b substr($b0, --$y);

      return 
    $b;
    }

    // test strings in an array
    $text_with_separators = array
         (
                   
    "{available_yesterday} this text I need to extract {/available_yesterday}",
                   
    "{available_now}       this text I need to extract {/available_now}",
                   
    "{available_tomorrow}  this text I need to extract {/available_tomorrow}",
         );

    // iterate each string and show results
    foreach($text_with_separators as $item)
    {
        echo 
    $item .'<br />'
        
        
    $b _extract($item);

        echo 
    $b .'<br /><br />';


    // output results
    {available_yesterday} this text I need to extract {/available_yesterday}
    this text I need to extract

    {available_now} this text I need to extract {/available_now}
    this text I need to extract

    {available_tomorrow} this text I need to extract {/available_tomorrow}
    this text I need to extract
    Last edited by John_Betong; Nov 7, 2012 at 09:11. Reason: added output results
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014


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
  •