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 3665432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:45:38+00:00 2026-05-19T01:45:38+00:00

I have an html document that I saved as a .txt file. I want

  • 0

I have an html document that I saved as a .txt file. I want to extract each string following /user/ and make a comma-separated list of all the extracted strings.
So every time there’s a “/user/boy34” in this txt file, I would like to extract the “boy34” part. Im really new to PHP but I’ve been reading about the preg_match_all() function and I think that’s what I need to use.

Here’s what I’ve come up so far but it doesn’t work:

<?php
$str = file_get_contents("comment.txt");
preg_match_all ('/^(user\/)\/[A-Z0-9][A-Z0-9_-]+\"$/i', $str, $preg);
print_r ($preg);
?>

The output I get from this is:

Array ( [0] => Array ( ) [1] => Array ( ) ) 

Can somebody please help me?

  • 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-05-19T01:45:39+00:00Added an answer on May 19, 2026 at 1:45 am

    Using ^ in a regex means that it will only match if the entire line begins with your subject. Also, the $ at the end means the line must also end right after the match. So you will never find anything, unless the entire line is nothing but /user/boy34. Also, you probably need the m flag for multiline mode.

    You should also use the shortcuts, like \w (word characters, A-Za-z0-9_)

    Try out this regex pattern: /"\/user\/(\w+)"/im

    If you post an example of your HTML, I can actually test this out and get you a working regex pattern.

    — UPDATE —

    I tested using this HTML:

    <html>
      <body>
        <a href="/user/boy30" />
        <a href="/user/boy31" />
        <a href="/user/boy32" />
      </body>
    </html>
    

    and the regex mentioned above, and I got it to work in this very simple test. I used this site to test: http://www.spaweditor.com/scripts/regex/index.php

    Here were my results:

    Array
    (
        [0] => Array
            (
                [0] => "/user/boy30"
                [1] => "/user/boy31"
                [2] => "/user/boy32"
            )
    
        [1] => Array
            (
                [0] => boy30
                [1] => boy31
                [2] => boy32
            )
    
    )
    

    — Regex Explanation —

    • / Required to start any regex pattern
    • " Looks for a double-quote character
    • \/user\/ Searches for /user/ (the forward-slashes needed to be escaped)
    • ( Anything between parenthesis will be grouped together in your results (leaving the parenthesis out will not break the regex, it will still find the matches, but this allows us to extract “boy32” up front.)
      • \w+ Searches for 1 or more (+ means “1 or more”) word characters (equivalent to [a-zA-Z0-9_])
      • ) Ends the grouping started before
    • " Looks for another double-quote character
    • / Required at the end of any regex pattern, and before any flags
      • i Flag: Case-Insensitive Mode
      • m Flag: Multi-Line Mode (normally, line-breaks will terminate expressions, this allows the pattern to match even over multiple lines)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML document that is using to embed a control. In some
I have an HTML (not XHTML) document that renders fine in Firefox 3 and
I have an <img> in an HTML document that I would like to highlight
I am constructing a large HTML document from fragments supplied by users that have
I have a word document that was saved as xml. now I need to
I have a site that saves user documents and I want to use Paperclip
I have an html document that is included as an iframe in a toolbar.
I have HTML that includes symbols such as the Trademark TM as superscript (™).
I have html code that looks roughly like this: <div id=id1> <div id=id2> <p>some
Do WYSIWYG editors generally have problems editing HTML documents that has been exported by

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.