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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts

    return strings within a string

    Hi,

    I am having issues writing a function (i'm fairly new with programming).. I have strings like this:

    Code:
    http://www.asdf.com/$doh/
    http://www.url.com/$foo/$bar/
    http://www.thingy.com/$doe/$rae/$mee/
    How can I get return what the substrings after and including the dollar sign up to the slash?

    I figured out a couple of things, like I can use substr_count to count the dollar signs, then I'd have to iterate through the string the amount of times the $ is found, but I kept getting the same result.

    Any PHP pro's who could shed some light/links would be greatly appreciated.

    Cheers

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Use pattern matching, its more overhead but its easier to use. From the sounds of it, it would actually be less overhead than going through each value one by one. I'm not sure if you meant the pattern should include the / or not, so I've got both for you.
    PHP Code:
    $s 'http://www.thingy.com/$doe/$rae/$mee/';
    $pattern '/(\$.*)\//U'// Without the last slash
    $pattern '/(\$.*\/)/U'// With the last slash
    $match = array();
    preg_match_all($pattern$s$match); 
    Results are stored in $match[1], so you can count them with count($match[1]), and access each piece from the arrays in $match[1] ($match[1][0], $match[1][1], $match[1][2]...).

    Edit:
    Sorry forgot to mention. These patterns require that the ending slash exists. To get around that, you need to set an optional for the \/ in either piece.
    Last edited by Fou-Lu; 07-16-2008 at 09:11 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    samuurai (07-18-2008)

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    That's awesome... worked like a charm! It ended up retaining the trailing slash with both regexes though.. I just trimmed it of though.

    Thanks a lot

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You can't use both patterns as shown above. The second pattern includes the trailing slash and that pattern overwrites the original pattern. Just comment out the second $pattern = line and you should get it without the ending slashes.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    samuurai (07-19-2008)

  • #5
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    Code:
    <?php
    
    $string = 'http://www.thingy.com/$doe/$rae/$mee/';
    
    $pattern = '/(\$.*)\//U';
    
    $match = array();
    
    preg_match_all ($pattern, $string, $match);
    
    foreach($match[0] as $m) {echo $m . "\n";}
    
    ?>
    Returns
    $doe/ $rae/ $mee/

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Code:
    foreach ($match[1] AS $m)
    {
        echo $m . "<br />\n";
    }
    The 0 index always matches the full match given, but the pattern contains subpatterns within it (\$.*) which will be a multidimensional array at index 1.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    samuurai (07-21-2008)

  • #7
    Regular Coder
    Join Date
    Nov 2007
    Posts
    144
    Thanks
    64
    Thanked 0 Times in 0 Posts
    Great! thanks a lot.. I've been playing with RegexBuddy to see how these regex patterns work. Thanks for your help!

    Beren


  •  

    Posting Permissions

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