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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:22:10+00:00 2026-06-10T15:22:10+00:00

having issue with the explode, i have simple text file and each line is

  • 0

having issue with the explode, i have simple text file and each line is end with number. i just want to extract end of line number and create same number of sub stings. i tried to use explode function but unable to retrieve only end of string number. following is .txt file structure and my code with expected result.

‘text.txt’

a1 bbb ccc 111-111 ddd eee 3
a2 fff ccc (123-12312) 111-111 ddd eee 2
a3 bbb aaa (32 32)111-111 ddd eee 4

<?php  
    $file = "C:\\test.txt";  
    $f = fopen($file, "r");  
    while ( $line = fgets($f, 1000) ) {  
        print "<br/>";  
        $pieces = explode(" ", $line);  
        print $line;  
        print "<br/>";  
        print "<\t>";  
        for(i=0; i>=$pieces[5];i++)  
        {  
            print "<br/>";  
            print "<\t>";  
        }  
        print "<br/>";  
     }  
?  

Output:
a1

  1. sub string
  2. sub string
  3. sub string

a2
1. sub string
2. sub string

a3
1. sub string
2. sub string
3. sub string
4. sub string

  • 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-10T15:22:12+00:00Added an answer on June 10, 2026 at 3:22 pm

    You could also just use substr()

    echo substr(trim($line), -1);

    Tho I would do it like:

    <?php
    $file = "./test.txt";
    
    foreach(file($file) as $lines){
        echo '<ol>'.PHP_EOL;
        $i = 1;
        foreach($parts = explode(' ',trim($lines)) as $key=>$line){
            if($key == 0){echo "\t<p>".$line.'</p>'.PHP_EOL;}
            if($key >= 1 && $key <= end($parts)){echo  "\t<li>".$line.'</li>'.PHP_EOL;}
            $i++;
        }
        echo '</ol>'.PHP_EOL;
    }
    /* Result
    <ol>
        <p>a1</p>
        <li>bbb</li>
        <li>ccc</li>
        <li>111-111</li>
    </ol>
    <ol>
        <p>a2</p>
        <li>fff</li>
        <li>ccc</li>
    </ol>
    <ol>
        <p>a3</p>
        <li>bbb</li>
        <li>aaa</li>
        <li>(32</li>
        <li>32)111-111</li>
    </ol>
    
    */
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just started mongo and started having issue with querying already. i have a collection
Hi guys I am having issue I have this query: SELECT * FROM useraccount
Having an issue here that I have tried everything I can think of but
I'm having an issue with a simple PHP mailer. I've had this script working,
We have a device that support SAE J1939 interface and having issue with finding
I am having issue with Google line chart API. When I reload the div,
Having issue with my navigation. How can I prevent it? My CSS file looks
Im having issue with mail sending In web.config file <mailSettings> <smtp from=xxxx@companyname.com> <network host=mail.authsmtp.com
I am having issue with loading a html textarea via ajax, essentially the text
I am having issue with git pull.I have commited my changes in local repo.

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.