Reputation: 31
I have a long string as a single line (its too long) and I want it to break and print as a new line whenever we meet a semi colon ;
and }
or {
for example
This is my string:
aaaaaaaaaaa;bbbbbbbbbb{ccccccccc;dddddddd;}eeeeeee{fffffff;}
I want to print it as below:
aaaaaaaaaaa;
bbbbbbbbbb{
ccccccccc;
dddddddd;
}
eeeeeeee{
fffffffffff;
}
Even if ;
and }
meet together I wanna break it down and print in two lines.
For example jjjjjjjjjjjjj;}
might display as
jjjjjjjjjjj;
}
Please help me.
Upvotes: 1
Views: 60
Reputation: 581
You can use like the below code also :
$str = 'aaaaaaaaaaa;bbbbbbbbbb{ccccccccc;dddddddd;}eeeeeee{fffffff;}';
$arr_replace_char = array(';','{','}');
$arr_replace_by = array(';<br>','{<br>','}<br>');
echo str_replace($arr_replace_char,$arr_replace_by,$str);
Upvotes: 0
Reputation: 43441
You can use preg_replace(/([;\{\}])/, '$1<br/>', $sourceLine)
for HTML output. If outputing to file, change '$1<br/>'
to "$1\r\n"
Upvotes: 1
Reputation: 21437
You can simply use preg_replace
as
$str = 'aaaaaaaaaaa;bbbbbbbbbb{ccccccccc;dddddddd;}eeeeeee{fffffff;}';
$res = preg_replace('/(;|{|})/',"$1\n",$str);
print_r($res);
Output :
aaaaaaaaaaa;
bbbbbbbbbb{
ccccccccc;
dddddddd;
}
eeeeeeee{
fffffffffff;
}
Upvotes: 3