faq
faq

Reputation: 3076

A string replace problem

$string = '1.this is the first2.second3.and thethird';
$string = str_replace('??', '<br> ??', $string);
echo $string; 
//output: 
1.this is the first <br>
2.second <br>
3.and thethird

What str_replace do i need? please note that the frist number on output has no <br> tag. thanks

Upvotes: 0

Views: 67

Answers (2)

Landon
Landon

Reputation: 4108

I don't know how much control you have over the input string, but using explode() is a lot easier and cleaner. The only requirement is you need to be able to put delimiters in your string.

$string = '1.this is the first|2.second|3.and thethird';
$array=explode('|',$string);
foreach($line as $array){
   echo $line."<br>";
}

Upvotes: -1

Frank Farmer
Frank Farmer

Reputation: 39356

$ cat scratch.php
<?php
$string = '1.this is the first2.second3.and thethird';
$string = preg_replace('/([^0-9])([0-9]+\.)/', "\$1 <br>\n\$2", $string);
echo $string; 



$ php scratch.php | more
1.this is the first <br>
2.second <br>
3.and thethird



$ 

Upvotes: 2

Related Questions