barrycarlyon has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Estemmed Monks

I bleieve the title is correct

Given a input:

Hello, my name is barry I am 20 I am webmaster of lsrfm.com its a student radio station

What I want to to is, remove the return carriages and repalce them with <br&rt; or </p&rt;<p&rt;

So could a fellow monk advise on how to search a ref. and replace, and also what should I search for as, I am not sure what the return carriage character is

Further more the input listed is in:

my $text = "above"

So, basic program

my $input = "Hello, my name is barry I am 20 I am webmaster of lsrfm.com its a student radio station"; 'some process replacing return chars. with &lt;/p&rt;&lt;p&tk;'

As when placing the text on a page, intially obtained from a form on a webpage, where it is a textarea, it does not format the same way as it does in the text area.
Similar to this perl monks text area, but the users dont put html tags in!

Yours

Barry Carlyon barry@barrycarlyon.co.uk

Replies are listed 'Best First'.
Re: Search a perl reference and replace characters
by davido (Cardinal) on Jun 01, 2006 at 21:23 UTC

    Further more the input listed is in:

    my $text = "above"

    Huh?

    Do you wish to convert 'newlines', or 'carriage returns'? My guess is probably newlines. If that's the case, you may be looking for something like this:

    $input =~ s{\n}{&lt;/p&rt;&lt;p&tk;}g

    Or maybe you really do mean carriage returns, in which case it would be:

    $input =~ s{\r}{&lt;/p&rt;&lt;p&tk;}g

    Your entities look funny to me though, but that's a different issue.

    Regular expressions, including 'substitutions' are covered in perlre, perlrequick, and perlretut. Those documents will go a long way toward clearing the haze regarding string manipulation and pattern matching in Perl. They're well worth the time they take to read.


    Dave

      my guess (from the context) is that OP has typos repeatedly; that intent was for each of those
        &rt;
      was meant to be:
        &gt;
      I always go for the safe approach ...
      Pseudocode ...
      Find Replace chr(13).chr(10) with "<br>";
      Find Replace chr(13) with "<br>";
      Find Replace chr(10) with "<br>";

      This seem to cover Winslows, Mac and Unix flavours of text input ...
Re: Search a perl reference and replace characters
by GrandFather (Saint) on Jun 01, 2006 at 23:06 UTC

    The following seems to be what you are after:

    use strict; use warnings; my @lines = <DATA>; chomp @lines; # Strip line end characters print '<p>', join ("</p>\n<p>", @lines), "</p>\n"; __DATA__ Hello, my name is barry I am 20 I am webmaster of lsrfm.com its a student radio station

    Prints:

    <p>Hello,</p> <p>my name is barry</p> <p>I am 20</p> <p>I am webmaster of lsrfm.com</p> <p>its a student radio station</p>

    Note that line ends are put back into the generated HTML so it is easier to read. They make no difference to the rendering.


    DWIM is Perl's answer to Gödel
Re: Search a perl reference and replace characters
by jdporter (Paladin) on Jun 01, 2006 at 22:47 UTC
    'some process replacing return chars. with (</p><p>)' =~ s(.*(</p><p>).*){\$input =~ s#\n#$1#g;};
    We're building the house of the future together.