Need preg_replace help

Discussion in 'PHP' started by qwikad.com, Jul 28, 2024.

  1. #1
    I have links that vary and look something like this:

    somesite/articles/25/news/15/1/141720.html

    somesite/articles/10/25/updates/3/1/1413410.html

    somesite/makers/16/11/press/1/7/145623.html

    etc. etc.

    I need to strip / preg_replace everything except these (the html IDs at the end of the links):

    141720.html
    1413410.html
    145623.html

    etc. etc.

    They always vary too. So... Is there a way? I was thinking stripping everything before the last slash forward, but then what? How do you get those html IDs?

    Thank you for any help.
     
    qwikad.com, Jul 28, 2024 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,816
    Likes Received:
    4,536
    Best Answers:
    123
    Trophy Points:
    665
    #2
    I don't like regular expressions so I tend to use explode and then just get the last array element - efficient and readable. But there is a way to regex results into an array and select the last item too.
     
    sarahk, Jul 28, 2024 IP
    qwikad.com likes this.
  3. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,283
    Likes Received:
    1,697
    Best Answers:
    31
    Trophy Points:
    475
    #3
    Glad you said that. I'd be still going around in circles if I went the preg_replace route.
     
    qwikad.com, Jul 29, 2024 IP
  4. zaidzahid

    zaidzahid Active Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    80
    #4
    You may use string functions basename() and str_replace to do that you might use basename function to get end part of url and than use str_replace to remove it.
     
    zaidzahid, Jul 29, 2024 IP