SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    Working on it... Contrid's Avatar
    Join Date
    Apr 2006
    Location
    Online
    Posts
    955
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    RegEx - Extract Number

    Hey there,

    Let's say I have a string like this :

    HTML Code:
    <!--productCat__36-->
    Imagine that this string is inside of a block of text content. There are several of these strings, and I need to somehow extract the number, such as "36" in this case from the strings.

    Which is the best way to do this?
    Any guidance will be greatly appreciated.
    And so I got lost in code...completely asphyxiated by it...

    Premium WordPress plugins - Tribulant Software

  2. #2
    Working on it... Contrid's Avatar
    Join Date
    Apr 2006
    Location
    Online
    Posts
    955
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I was thinking of something like :

    PHP Code:
    $string "this is my content with a string like <!--productCat__36--> this in it";
    $number ereg_replace("[^0-9]"""$string);
    echo 
    $number
    ...but I'm just a little confused.
    How will I do this with multiple instances of the specified string?

    while() maybe?
    And so I got lost in code...completely asphyxiated by it...

    Premium WordPress plugins - Tribulant Software

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    make an array of those strings, and foreach ($strs as $str) { // echo the number }

  4. #4
    Working on it... Contrid's Avatar
    Join Date
    Apr 2006
    Location
    Online
    Posts
    955
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I have figured out a solution...though there is a small bug.
    Let's say I have all those strings in an array and loop through them, it won't cater for multiple instances of the string in the content. What do you think?
    And so I got lost in code...completely asphyxiated by it...

    Premium WordPress plugins - Tribulant Software

  5. #5
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you really just cared about numbers ...

    preg_split('/\D/', $str)

    But I'd expect something more like ...

    preg_match_all('/productCat__(\d+)/', $str, $this_array_has_what_you_need)
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  6. #6
    Working on it... Contrid's Avatar
    Join Date
    Apr 2006
    Location
    Online
    Posts
    955
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the responses guys!
    These regular expressions can be confusing sometimes.
    And so I got lost in code...completely asphyxiated by it...

    Premium WordPress plugins - Tribulant Software


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
  •