3 time numbering per line

$var="This is a book.
I am a boy.
What is your name?
Good morning!
hello, Tom!";

$var='
'. $var;
$var=str_replace(chr(10), chr(10).'(^)', $var);

$token=strtok($var, '^');

$counter=0;
$numbering='';
while ($token != false):
$numbering .=$counter++. $token;
$token=strtok('^');
endwhile;

$numbering=str_replace('0', '', $numbering);
echo $numbering;

The code above produces the result below.

I like to make my target result below.

The code below is one of my trials for my target result above.

$var=str_replace(chr(10), chr(10).'(^)[^]{^}', $var);

However, it produces the result below.

You should look at the explode and implode methods.

Hi @joon1, could you please share what the point of this is? Are you just trying to learn something or is there some problem this is supposed to solve?

I like to make a link per each line.

The result above is the simplified form of my target result below.

<a href="#1">1</a><div id="1"></div>This is a book.
<a href="#2">2</a><div id="1"></div>I am a boy.
<a href="#3">3</a><div id="1"></div>What is your name?
<a href="#4">4</a><div id="1"></div>Good morning!
<a href="#5">5</a><div id="1"></div>hello, Tom!

Tthe numbers are added by 3 times per line like the following is the problem what I want solve.

<a href="#1">2</a><div id="3"></div>This is a book.
<a href="#4">5</a><div id="6"></div>I am a boy.
<a href="#7">8</a><div id="9"></div>What is your name?
<a href="#10">11</a><div id="12"></div>Good morning!
<a href="#13">14</a><div id="15"></div>hello, Tom!

Is there any reason the $var data needs to be a string? If you make it an array, getting the result you want is quite trivial.

Are you saying you want the links and id’s incremented by 3 or you don’t want that? And if you do want that, why?

Why do you have empty div’s?

I am not really sure what your up to but this may help.

<?php

 $arr = [
   1 => 'This is a book.'
 , 2 => 'I am a boy.'
 , 3 => 'What is your name?'
 , 4 => 'Good morning!'
 , 5 => 'hello, Tom!'
  ];

  foreach ($arr as $key =>$value){
   echo "<a href='#$key'>$key $value</a><br>\n";

  }

I am sorry my target result which is posted in #4 has some typo errors.
The following is my target restul.

<a href="#1">1</a><div id="1"></div>This is a book.
<a href="#2">2</a><div id="2"></div>I am a boy.
<a href="#3">3</a><div id="3"></div>What is your name?
<a href="#4">4</a><div id="4"></div>Good morning!
<a href="#5">5</a><div id="5"></div>hello, Tom!

I modified my target result for removing the empty div like the following.

<a href="#1" id="1">1</a>This is a book.
<a href="#2" id="2">2</a>I am a boy.
<a href="#3" id="3">3</a>What is your name?
<a href="#4" id="4">4</a>Good morning!
<a href="#5" id="5">5</a>hello, Tom!

Hi @joon1,

Perhaps you may be interested in the following:

Standing on the shoulder’s of giants - @banamen’s post#5

<?php
declare(strict_types=1);
# echo error_reporting() .ini_get('display_errors') .'<hr>';

$arr = [
   1 => 'This is a book.'
 , 2 => 'I am a boy.'
 , 3 => 'What is your name?'
 , 4 => 'Good morning!'
 , 5 => 'hello, Tom!'
  ];

$arr = [
  [
    'This is a book.'
   ,"title goes here \n\twith tab \nnew line no tab"
   ,'alt goes here'
  ], [
   'I am a boy.'
   ,"title goes here \n\twith tab \nnew line no tab"
   ,'alt goes here'
  ],  
  [
    'What is your name?'
   ,"title goes here \n\twith tab \nnew line no tab"
   ,'alt goes here'
  ], [  
   'Good morning!'
   ,"title goes here \n\twith tab \nnew line no tab"
   ,'alt goes here'
  ], [ 
   'hello, Tom!'
   ,"title goes here \n\twith tab \nnew line no tab"
   ,'alt goes here'
  ],
];


  # echo '<pre>'; print_r($arr); echo '</pre><hr>';
  foreach ($arr as $key =>$value)
  {
    # echo '<pre>'; print_r($value); echo '</pre><hr>'; die;

    $url = str_replace(' ', '-', $value[0]);
    $url = strtolower($url);

    echo <<< ____EOT
      #$key ==> 
      <a 
        href='{$url}.html' 
        title="$value[1]" 
        alt="$value[2]" 
      >
        $value[0]
      </a>
      <br>
____EOT;
  }

[off-topic]

Hi @joon1,
Quite some time ago you mentioned how you managed to connect your computer to the internet and to use your own domain. At the time I was busy and did not manage to get it to work. I would be grateful if you could raise another topic and describe in detail the steps to achieve creating the following:

http://form.kr/test01/t12.php#10

[/off-topic]

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.