Domanda

I am having issues with <BR /> tags appearing inside of a pre-populated textarea form (with no breaks obviously). So these break tags need to be converted to /n within the textarea. This prepopulated text is being retrieved via the URL which also appears to have the break tags within it as well.

Basically what is going on and what I need:

I am having to get a block of text from the previous page (which was originally pulled from the database) and load that block of text into a textarea field on the form submission page. The user then fills out the rest of the form fields and submits the form, this information is then stored into a specific table in the database.

In order to get the block of text to display properly on the first page, I had to use nl2br to get this section breaking properly on the page. Obviously, now it is outputting that text with breaks instead of new lines.

This means when you click on the link to populate the form on the next page, the link has break tags in it, which in turn gets displayed inside of the textarea field on the next page as well as in the URL.

I am confused on what I need to do to replace those <BR />'s with /n again. It seems like I could use str_replace or preg_replace, but I have yet to find an example of how to use either of those in conjunction with something like:

<textarea rows="10" cols="50"><? if(isset($_GET['text-property'])){ echo $_GET['text-property']; }?></textarea>

Keep in mind that I am still in the process of learning PHP, so I really need specifics and preferably examples if at all possible.

Thanks!

È stato utile?

Soluzione

You need to use preg_replace,

<textarea rows="10" cols="50"><? if(isset($_GET['text-property'])){ echo preg_replace('/<br[^>]*?>/si', '\n',$_GET['text-property']); }?></textarea>

Demo

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top