Remove multiple n in PHP String from beginning and end of a string in this somewhat unique case

I am stuck with figuring out how to properly fix formatting from an older database.

Note, that just trimming the data or removing all n’s is not what I am looking for, I need it a bit more complex – which is where I struggle to figure out.

I tried different scripts I found online, tampering with them, rewriting them, testing a lot, and nothing really works the way I need it to :/ I just can not figure out the logic how to write a script to do that


So I have 5000+ strings I need to do this on, so I need a solid function to use to loop through all of it. Here is what I am trying to do:

I need to remove all formatting (just multiple n or one n or mix) from beginning of the string, and from the end of the string. Let me post 3 examples of the strings (pulled from db to make it simple to do tests):

<?php
$data = 'n n Member of the public council of Enlightening Movement Enlightenment Movementn Fakori Beheshti (20160517)n MP Raihana Azad (20170910)n n The &quot;Enlightening movement&quot; is a Hazara controlled social movement promoting Hazara affairs in Afghanistan. It has international supporters mostly Hazara Expatriates.n n Background:n n Declaration by the intellectuals from Afghanistan in support of the &ldquo;Enlightening movement&rdquo;.n n May 8, 2016n n Translated by Hazara People International Networkn n In the name of God of wisdom and enlightenmentn.';

$data2 = 'n            Provicial Council Uruzgan 8 Members 20090820 Elections:Mohammad Aman Hotak Amanullah Hotaki was the PC Chief (20110114). He was replaced by Ibrahim Akhunzada Januar 2012. who accused his predecessor of misappropriating the council’s budget. Akhunzada escaped unhurt in a roadside bombing in Dehrawud District, Uruzgan province on 20121030.nnnnnnnnnnnnBiographies of Uruzgan provincial council membersnnnnnn1-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Amanullah Hotaki: Amanullah Hotaki is the son of Malik Gulam Jan. He is born in 1981 at Nawabad village of Dihrawud district..6-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Jan Mohammad Karimi: Jan Mohammad Karimi is the son of Alhaj Tal. He is born in 1977 in Dazak village of Dihrawud district.He studied up to twelfth class but has no political affiliation.&nbsp;nnnnnnnnnnnnn';

$data3 = 'n 1. Chairman Afghanistan Geodesy and Cartography AGCHO: Eng. Abdul Raouf Yari (20080503)nalso in Afghanistan Geodesy also Cartography: Eng. Abd. Wasi, Amanullah Afshar (20091012)nHead of Department of Geography:Prof. Mohammad Zarif Taniwal (20091012)';

$data4 = 'n n 20. Balkh: has 11 seats including three for women. The elected candidates represent a total of 97,727 votes.nn n n n n Non ';

$data_fixed = no_single_function_worked_on_all_4_examples($data);
echo data_fixed;
?>

So I want to clean up the beginning spacing of n, and the ones at the end. So that the string starts and ends with just nothing, and the spacing of n in middle of content stay intact..

So that result would be like this:

$data = 'Member of the public council of Enlightening Movement Enlightenment Movementn Fakori Beheshti (20160517)n MP Raihana Azad (20170910)n n The &quot;Enlightening movement&quot; is a Hazara controlled social movement promoting Hazara affairs in Afghanistan. It has international supporters mostly Hazara Expatriates.n n Background:n n Declaration by the intellectuals from Afghanistan in support of the &ldquo;Enlightening movement&rdquo;.n n May 8, 2016n n Translated by Hazara People International Networkn n In the name of God of wisdom and enlightenment';
$data2 = 'Provicial Council Uruzgan 8 Members 20090820 Elections:Mohammad Aman Hotak Amanullah Hotaki was the PC Chief (20110114). He was replaced by Ibrahim Akhunzada Januar 2012. who accused his predecessor of misappropriating the council’s budget. Akhunzada escaped unhurt in a roadside bombing in Dehrawud District, Uruzgan province on 20121030.nnnnnnnnnnnnBiographies of Uruzgan provincial council membersnnnnnn1-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Amanullah Hotaki: Amanullah Hotaki is the son of Malik Gulam Jan. He is born in 1981 at Nawabad village of Dihrawud district..6-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Jan Mohammad Karimi: Jan Mohammad Karimi is the son of Alhaj Tal. He is born in 1977 in Dazak village of Dihrawud district.He studied up to twelfth class but has no political affiliation.&nbsp';

Please someone help me to write a function to parse these strings in that way. I can not figure out the PHP Logic to get it done, and removing all formatting is not an option 🙁