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.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post

    Grabbing random text out of a string..

    Hey guys.... was wondering if anyone had any ideas for this one.... say we had this text for example..

    Code:
    <some code>random text</some code>
    Now..... say I knew what came BEFORE and AFTER the "random text" part; how would I go about pulling out the random text?

    Thanks for any help!

  • #2
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Not sure what you mean but:

    PHP Code:
    <?php

    $str 
    'Some text in this variable';
    $parts explode(' '$str);
    echo 
    array_rand($parts);

    ?>

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Maybe something like this:

    PHP Code:
    <?php

    $string 
    '<some code>random text here</some code>';

    $match '/\<some code\>(.+?)\<\/some code\>/is';
    $randomText preg_replace$match"$1"$string );

    ?>

  • #4
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Thanks guys.... yeah; was thinking about how to do it after I posted this & figured that using preg_replace() would probably be the best way to go about doing it.

    Missing-score..... can you tell me what the "$1" parameter is?

    Thanks!

  • #5
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    $1 will give the contents of the pattern matched by (.+?)
    Omnis mico antequam dominus Spookster!

  • #6
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For every bracket set you use in a regular expression (eg: (.+?), ([a-z0-9]+)), you can access the contents of that specific section with a number. In that case, the first (and only) bracket group was retrieved using $1.

  • #7
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Oh ok... thx!

    What about these parts..

    $match = '/\<some code\>(.+?)\<\/some code\>/is';
    I didn't think ">" needed to be escaped? Thought it was only.. "[ . \ * ? +" (except in some cases such as class expressions) and "- ^ $" in some circumstances.

    Doesn't using "\<" anchor the next RE to the start of a word & "\>" anchor the previous RE to the end of a word?"

    Oh & what does "is" mean at the end there? I know that "i" makes it case-insensitive; but not sure what the added "s" does?

    Thanks!
    Last edited by cyphix; 02-13-2006 at 12:19 PM.

  • #8
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It depends what delimiter you use I believe. You can use more than / to start a regex pattern. To be honest, my regex is moderatley limited, Ive only ever really worked with the / delimiter.

  • #9
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Oh ok.... thanks! Btw... I edited my post slightly after you posted I believe..

  • #10
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This page may help you understand the modifiers:

    http://uk2.php.net/manual/en/referen....modifiers.php

  • #11
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Alright.. TY!

  • #12
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by cyphix
    Alright.. TY!
    No problem, feel free to ask any other questions you may have


  •  

    Posting Permissions

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