Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8828639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:39:11+00:00 2026-06-14T07:39:11+00:00

I am trying to change all the links of a html with php preg_replace.

  • 0

I am trying to change all the links of a html with php preg_replace.
All the uris have the following form

http://example.com/page/58977?forum=60534#comment-60534

I want to change it to:

http://example.com/60534

which means removing everything after “page” and before “comment-“, including these two strings.

I tried the following, but it returns no changes:

$result = preg_replace("/^.page.*.comment-.$/", "", $html);

but it seems that my regex syntax is not correct, as it returns the html unchanged.
Could you please help me with this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:39:12+00:00Added an answer on June 14, 2026 at 7:39 am

    The ^ is an anchor that only matches the start of the string, and $ only matches at the end. In order to match you should not anchor the regular expression:

    $result = preg_replace("/page.*?comment-/", "", $html);   
    

    Note that this could match things that are not URLs. You may want to be more specific as to what will be replaced, for example you might want to only replace links starting with either http: or https: and that don’t contain whitespace.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I´m trying to change a spring jsp example to use freemarker. I changed all
I have the html below: <span class=srch-URL> <a id=CSR_U_2 href=http://www.test.com/TestForm.aspx>http://www.test.com/TestForm.aspx</a> </span> I am trying
I'm trying to split some html content using php's preg-match-all function: <li class=cat-item><a title=blabla
Im trying to change title style for all links however i just found how
I am trying to change all my .live() to .on() as the prior is
I'm trying to change the owner of a folder (myFolder) and all its content
I am trying to change the menuOption background-color on hover using JQuery. All my
As the title says, I'm trying to change the ID of all elements with
I am trying to get what all fields(input,select) are changed during keyup and change
I'm trying to iterate through all the empty textboxes in a table and change

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.