SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Bangladesh
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Extract specific substring from a string

    Hi,

    Hope all are well. I wanna know one simple thing, may be simple but I don't know.

    Explain by example: Say we have a string like as follows:
    PHP Code:
    $str 'This is an article{content inside second bracket} that is related to {content inside second bracket} PHP'
    How can I detect and extract the part inside second bracket [content inside second bracket]?

    Any idea to achieve it?? preg_match() or anything else.

    Thanks for seeing my post.
    Rashidul

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $string 
    'This is an article{content inside first bracket} that is related to {content inside second bracket} PHP';

    preg_match_all(
      
    '~(?<={).+?(?=})~',
      
    $string,
      
    $matches
    );

    print_r(
      
    $matches
    );

    /*
      Array
      (
        [0] => Array
        (
          [0] => content inside first bracket
          [1] => content inside second bracket
        )
      )
    */
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Bangladesh
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Anthony for ur Quick response. I appreciate ur effort.

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Bangladesh
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    PHP Code:
    <?php
    $string 
    'This is an article{content inside first bracket} that is related to {content inside second bracket} PHP';

    preg_match_all(
      
    '~(?<={).+?(?=})~',
      
    $string,
      
    $matches
    );

    print_r(
      
    $matches
    );

    /*
      Array
      (
        [0] => Array
        (
          [0] => content inside first bracket
          [1] => content inside second bracket
        )
      )
    */
    Hi,

    If i want to replace the actual string looks like
    PHP Code:
    $string 'This is an article{#1} that is related to {#2} PHP'
    First match: #1
    2nd Match: #2
    and so on.

    How can I do this?

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,094
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Anthony's code works just as well with the new string as it did with the old string.

    Try it:
    PHP Code:
    $string 'This is an article{#1} that is related to {#2} PHP'

    preg_match_all(
      
    '~(?<={).+?(?=})~',
      
    $string,
      
    $matches
    );

    print_r(
      
    $matches
    );

    /*
    Array
    (
        [0] => Array
            (
                [0] => #1
                [1] => #2
            )

    )
    */ 
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Bangladesh
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow

    Quote Originally Posted by ScallioXTX View Post
    Anthony's code works just as well with the new string as it did with the old string.

    Try it:
    PHP Code:
    $string 'This is an article{#1} that is related to {#2} PHP'

    preg_match_all(
      
    '~(?<={).+?(?=})~',
      
    $string,
      
    $matches
    );

    print_r(
      
    $matches
    );

    /*
    Array
    (
        [0] => Array
            (
                [0] => #1
                [1] => #2
            )

    )
    */ 
    Actually I want to do is as follows

    Actual string will looks like as
    PHP Code:
    $string 'This is an article{content inside first bracket} that is related to {content inside second bracket} PHP'
    After preg_match_all I want to see the string looks like as
    PHP Code:
    $string 'This is an article{#1} that is related to {#2} PHP'
    Is it possible?

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Bangladesh
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After preg_match_all or using preg_match_all I wanna do this

  8. #8
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So do you want to extract the characters between {} and replace them with something else or do you want to capture the characters between {} and do something absolutely different with those? The code provided works for capturing the substrings, and if you got stuck with replacing those with something else then you need to articulate yourself a bit clearer.

  9. #9
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Bangladesh
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by furicane View Post
    So do you want to extract the characters between {} and replace them with something else or do you want to capture the characters between {} and do something absolutely different with those? The code provided works for capturing the substrings, and if you got stuck with replacing those with something else then you need to articulate yourself a bit clearer.
    I want to replace the above string with numbering. First match will get number 1, second match will get number 2 and so on....
    Sample input:
    PHP Code:
    $string 'This is an article{content inside first bracket} that is related to {content inside second bracket} PHP'
    Sample output:
    PHP Code:
     echo $string// output : This is an article{#1} that is related to {#2} PHP 

  10. #10
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $string 'This is an article{content inside first bracket} that is related to {content inside second bracket} PHP';

    preg_match_all(
      
    '~(?<={).+?(?=})~',
      
    $string,
      
    $matches
    );

    $i 1;

    function 
    do_increment(&$i)
    {
        return 
    '#' $i++;
    }

    $string preg_replace("~(?<={).+?(?=})~e"'do_increment($i)'$string);

    var_dump($string); 

  11. #11
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Bangladesh
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by furicane View Post
    PHP Code:
    $string 'This is an article{content inside first bracket} that is related to {content inside second bracket} PHP';

    preg_match_all(
      
    '~(?<={).+?(?=})~',
      
    $string,
      
    $matches
    );

    $i 1;

    function 
    do_increment(&$i)
    {
        return 
    '#' $i++;
    }

    $string preg_replace("~(?<={).+?(?=})~e"'do_increment($i)'$string);

    var_dump($string); 
    Thanks. perfect!

    However would u plz suggest me from where I can learn the regular expression?? I know PHP programming very well but regex.

  12. #12
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't want to sound rude, but if someone showed you how to extract portion of a string, and if you couldn't reach a conclusion how to change that substring to something else - you might want to work more with your php because this can be done with looping trough $matches and using str_replace, which we can all agree - are basics of string manipulation.

    If you want to learn how to create your own regular expressions, good place to start is www.regular-expressions.info and of course, http://www.php.net/manual/en/ref.pcre.php.

    Good luck

  13. #13
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Bangladesh
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I actually wanted to do using regex. I know string manipulation.

    Thanks for ur suggestion.

  14. #14
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Bangladesh
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by furicane View Post
    I don't want to sound rude, but if someone showed you how to extract portion of a string, and if you couldn't reach a conclusion how to change that substring to something else - you might want to work more with your php because this can be done with looping trough $matches and using str_replace, which we can all agree - are basics of string manipulation.

    If you want to learn how to create your own regular expressions, good place to start is www.regular-expressions.info and of course, http://www.php.net/manual/en/ref.pcre.php.

    Good luck
    Hi,

    If I want to do something like as follows [second bracket also be removed]:
    sample input:
    PHP Code:
    $string 'This is an article{content inside first bracket} that is related to {content inside second bracket} PHP'
    Output:
    PHP Code:
    echo $string// output : This is an article #1 that is related to #2 PHP 
    How can I do this. Any idea??

  15. #15
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,094
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by rashidul View Post
    How can I do this. Any idea??
    The code provided by furicane in post #10 does exactly what you want. Have you tried that code?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  16. #16
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Bangladesh
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    The code provided by furicane in post #10 does exactly what you want. Have you tried that code?
    It gives output looks like:
    PHP Code:
     This is an article {#1} that is related to {#2} PHP 
    I need to remove the second bracket also.

    It should looks like:
    PHP Code:
    This is an article #1 that is related to #2 PHP 


Tags for this Thread

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
  •