selective replacements
I have this in one of my php scripts to take out line breaks:
$html=str_replace(array("\n","\r"),"",$html);
Now I would like to leave some line breaks.
For example, if $html has something like this:
"<textarea> default text\n with line \nbreaks </textarea>"
I would like to leave all the linebreaks between the <textarea> tags.
Does any one know of a regular expression that can do this ?
Re: selective replacements
"meltedown" <groups2 [at] reenie.org> wrote in message
news:5Nhdf.12304$Ur3.9768 [at] fe01.news.easynews.com...
>I have this in one of my php scripts to take out line breaks:
> $html=str_replace(array("\n","\r"),"",$html);
>
> Now I would like to leave some line breaks.
> For example, if $html has something like this:
> "<textarea> default text\n with line \nbreaks </textarea>"
> I would like to leave all the linebreaks between the <textarea> tags.
> Does any one know of a regular expression that can do this ?
alt.php is down the hall...
Matt
Re: selective replacements
Matt Garrish wrote:
> "meltedown" <groups2 [at] reenie.org> wrote in message
> news:5Nhdf.12304$Ur3.9768 [at] fe01.news.easynews.com...
>
>>I have this in one of my php scripts to take out line breaks:
>> $html=str_replace(array("\n","\r"),"",$html);
>>
>>Now I would like to leave some line breaks.
>>For example, if $html has something like this:
>>"<textarea> default text\n with line \nbreaks </textarea>"
>>I would like to leave all the linebreaks between the <textarea> tags.
>>Does any one know of a regular expression that can do this ?
>
>
> alt.php is down the hall...
>
> Matt
>
>
Don't be nervous just because I mentioned PHP. I'm looking for a regular
expression, and if its in a perl context, that's fine. I can just as
easily use it in PHP. Perhaps you don't know this, but the syntax of
regular expressions in PHP is almost identical to that of perl. The
people in this newgroup are much much better at regular expressions than
the people in the PHP group, and they do not have an answer to this
question. I've already tried.
How do I replace all the line breaks in a string with '', except the
line breaks between <textarea> tags ? There's nothing unperl about that
question.
Re: selective replacements
"meltedown" <groups2 [at] reenie.org> wrote in message
news:Neqdf.19659$Ww6.10326 [at] fe05.news.easynews.com...
> Matt Garrish wrote:
>> "meltedown" <groups2 [at] reenie.org> wrote in message
>> news:5Nhdf.12304$Ur3.9768 [at] fe01.news.easynews.com...
>>
>>>I have this in one of my php scripts to take out line breaks:
>>> $html=str_replace(array("\n","\r"),"",$html);
>>>
>>>Now I would like to leave some line breaks.
>>>For example, if $html has something like this:
>>>"<textarea> default text\n with line \nbreaks </textarea>"
>>>I would like to leave all the linebreaks between the <textarea> tags.
>>>Does any one know of a regular expression that can do this ?
>>
>>
>> alt.php is down the hall...
>>
>
> Don't be nervous just because I mentioned PHP. I'm looking for a regular
> expression, and if its in a perl context, that's fine. I can just as
> easily use it in PHP. Perhaps you don't know this, but the syntax of
> regular expressions in PHP is almost identical to that of perl. The people
> in this newgroup are much much better at regular expressions than the
> people in the PHP group, and they do not have an answer to this question.
> I've already tried.
>
> How do I replace all the line breaks in a string with '', except the line
> breaks between <textarea> tags ? There's nothing unperl about that
> question.
The reason I refused to answer has nothing to do with anything you note
above, but that you should be using a parser and not a regular expression
(and for that you should be using the functionality provided by PHP and not
asking for help in a Perl group). The following is hack that will do what
you want, but hopefully will give you an idea about why you need to do more
than write a simple regular expression. (watch for wrapping on the regex)
my $string = <<STR;
php is no perl
<textarea>for reasons
that are too complicated
to explain here</textarea>
but you're free to google
them for yourself
STR
my $done;
$string =~ s#(.*?)(<textarea[^>]*>.*?</textarea>)?#my $txtarea=$2;(my $text
= $1)=~s/\n//;$done .="$text$txtarea"#gise;
print $done;
If you can't see why it's an abuse of the /e modifier then I wish you all
the luck in your programming future!
Matt
Re: selective replacements
Matt Garrish wrote:
> "meltedown" <groups2 [at] reenie.org> wrote in message
> news:Neqdf.19659$Ww6.10326 [at] fe05.news.easynews.com...
>
>>Matt Garrish wrote:
>>
>>>"meltedown" <groups2 [at] reenie.org> wrote in message
>>>news:5Nhdf.12304$Ur3.9768 [at] fe01.news.easynews.com...
>>>
>>>
>>>>I have this in one of my php scripts to take out line breaks:
>>>> $html=str_replace(array("\n","\r"),"",$html);
>>>>
>>>>Now I would like to leave some line breaks.
>>>>For example, if $html has something like this:
>>>>"<textarea> default text\n with line \nbreaks </textarea>"
>>>>I would like to leave all the linebreaks between the <textarea> tags.
>>>>Does any one know of a regular expression that can do this ?
>>>
>>>
>>>alt.php is down the hall...
>>>
>>
>>Don't be nervous just because I mentioned PHP. I'm looking for a regular
>>expression, and if its in a perl context, that's fine. I can just as
>>easily use it in PHP. Perhaps you don't know this, but the syntax of
>>regular expressions in PHP is almost identical to that of perl. The people
>>in this newgroup are much much better at regular expressions than the
>>people in the PHP group, and they do not have an answer to this question.
>>I've already tried.
>>
>>How do I replace all the line breaks in a string with '', except the line
>>breaks between <textarea> tags ? There's nothing unperl about that
>>question.
>
>
> The reason I refused to answer has nothing to do with anything you note
> above, but that you should be using a parser and not a regular expression
> (and for that you should be using the functionality provided by PHP and not
> asking for help in a Perl group).
> The following is hack that will do what
> you want, but hopefully will give you an idea about why you need to do more
> than write a simple regular expression. (watch for wrapping on the regex)
>
> my $string = <<STR;
> php is no perl
> <textarea>for reasons
> that are too complicated
> to explain here</textarea>
> but you're free to google
> them for yourself
> STR
>
> my $done;
> $string =~ s#(.*?)(<textarea[^>]*>.*?</textarea>)?#my $txtarea=$2;(my $text
> = $1)=~s/\n//;$done .="$text$txtarea"#gise;
> print $done;
>
Thanks, but isn't there supposed to be a semi colon after
$string =~ s#(.*?)(<textarea[^>]*>.*?</textarea>)?#
Is that a typo or is that supposed to be like that ?
I'm just trying to make sure I understand it right.
> If you can't see why it's an abuse of the /e modifier then I wish you all
> the luck in your programming future!
>
> Matt
>
>
Re: selective replacements
"meltedown" <groups2 [at] reenie.org> wrote in message
news:Wftdf.30220$xl1.27689 [at] fe03.news.easynews.com...
> Matt Garrish wrote:
>> "meltedown" <groups2 [at] reenie.org> wrote in message
>> news:Neqdf.19659$Ww6.10326 [at] fe05.news.easynews.com...
>>
>>>Matt Garrish wrote:
>>>
>>>>"meltedown" <groups2 [at] reenie.org> wrote in message
>>>>news:5Nhdf.12304$Ur3.9768 [at] fe01.news.easynews.com...
>>>>
>>>>
>>>>>I have this in one of my php scripts to take out line breaks:
>>>>> $html=str_replace(array("\n","\r"),"",$html);
>>>>>
>>>>>Now I would like to leave some line breaks.
>>>>>For example, if $html has something like this:
>>>>>"<textarea> default text\n with line \nbreaks </textarea>"
>>>>>I would like to leave all the linebreaks between the <textarea> tags.
>>>>>Does any one know of a regular expression that can do this ?
>>>>
>>>>
>>>>alt.php is down the hall...
>>>>
>>>
>>>Don't be nervous just because I mentioned PHP. I'm looking for a regular
>>>expression, and if its in a perl context, that's fine. I can just as
>>>easily use it in PHP. Perhaps you don't know this, but the syntax of
>>>regular expressions in PHP is almost identical to that of perl. The
>>>people in this newgroup are much much better at regular expressions than
>>>the people in the PHP group, and they do not have an answer to this
>>>question. I've already tried.
>>>
>>>How do I replace all the line breaks in a string with '', except the
>>>line breaks between <textarea> tags ? There's nothing unperl about that
>>>question.
>>
>>
>> The reason I refused to answer has nothing to do with anything you note
>> above, but that you should be using a parser and not a regular expression
>> (and for that you should be using the functionality provided by PHP and
>> not asking for help in a Perl group).
>
>> The following is hack that will do what you want, but hopefully will give
>> you an idea about why you need to do more than write a simple regular
>> expression. (watch for wrapping on the regex)
>>
>> my $string = <<STR;
>> php is no perl
>> <textarea>for reasons
>> that are too complicated
>> to explain here</textarea>
>> but you're free to google
>> them for yourself
>> STR
>>
>> my $done;
>> $string =~ s#(.*?)(<textarea[^>]*>.*?</textarea>)?#my $txtarea=$2;(my
>> $text = $1)=~s/\n//;$done .="$text$txtarea"#gise;
>> print $done;
>>
>
> Thanks, but isn't there supposed to be a semi colon after
> $string =~ s#(.*?)(<textarea[^>]*>.*?</textarea>)?#
>
> Is that a typo or is that supposed to be like that ?
> I'm just trying to make sure I understand it right.
>
No. Look up the /e switch in perlre.
Matt
Re: selective replacements
Matt Garrish wrote:
> "meltedown" <groups2 [at] reenie.org> wrote in message
> news:Wftdf.30220$xl1.27689 [at] fe03.news.easynews.com...
>
>>Matt Garrish wrote:
>>
>>>"meltedown" <groups2 [at] reenie.org> wrote in message
>>>news:Neqdf.19659$Ww6.10326 [at] fe05.news.easynews.com...
>>>
>>>
>>>>Matt Garrish wrote:
>>>>
>>>>
>>>>>"meltedown" <groups2 [at] reenie.org> wrote in message
>>>>>news:5Nhdf.12304$Ur3.9768 [at] fe01.news.easynews.com...
>>>>>
>>>>>
>>>>>
>>>>>>I have this in one of my php scripts to take out line breaks:
>>>>>>$html=str_replace(array("\n","\r"),"",$html);
>>>>>>
>>>>>>Now I would like to leave some line breaks.
>>>>>>For example, if $html has something like this:
>>>>>>"<textarea> default text\n with line \nbreaks </textarea>"
>>>>>>I would like to leave all the linebreaks between the <textarea> tags.
>>>>>>Does any one know of a regular expression that can do this ?
>>>>>
>>>>>
>>>>>alt.php is down the hall...
>>>>>
>>>>
>>>>Don't be nervous just because I mentioned PHP. I'm looking for a regular
>>>>expression, and if its in a perl context, that's fine. I can just as
>>>>easily use it in PHP. Perhaps you don't know this, but the syntax of
>>>>regular expressions in PHP is almost identical to that of perl. The
>>>>people in this newgroup are much much better at regular expressions than
>>>>the people in the PHP group, and they do not have an answer to this
>>>>question. I've already tried.
>>>>
>>>>How do I replace all the line breaks in a string with '', except the
>>>>line breaks between <textarea> tags ? There's nothing unperl about that
>>>>question.
>>>
>>>
>>>The reason I refused to answer has nothing to do with anything you note
>>>above, but that you should be using a parser and not a regular expression
>>>(and for that you should be using the functionality provided by PHP and
>>>not asking for help in a Perl group).
>>
>>>The following is hack that will do what you want, but hopefully will give
>>>you an idea about why you need to do more than write a simple regular
>>>expression. (watch for wrapping on the regex)
>>>
>>>my $string = <<STR;
>>>php is no perl
>>><textarea>for reasons
>>>that are too complicated
>>>to explain here</textarea>
>>>but you're free to google
>>>them for yourself
>>>STR
>>>
>>>my $done;
>>>$string =~ s#(.*?)(<textarea[^>]*>.*?</textarea>)?#my $txtarea=$2;(my
>>>$text = $1)=~s/\n//;$done .="$text$txtarea"#gise;
>>>print $done;
>>>
>>
>>Thanks, but isn't there supposed to be a semi colon after
>>$string =~ s#(.*?)(<textarea[^>]*>.*?</textarea>)?#
>>
>>Is that a typo or is that supposed to be like that ?
>>I'm just trying to make sure I understand it right.
>>
>
>
> No. Look up the /e switch in perlre.
>
> Matt
>
>
Ok. Thanks for clarifying. You've given me an idea by suggesting a
parser. I do have my own html parser and I think I can adjust it so
instead of just taking out all the linebreaks, it takes them out on a
tag by tag basis.
Re: selective replacements
In article <Wftdf.30220$xl1.27689 [at] fe03.news.easynews.com>, meltedown
<groups2 [at] reenie.org> wrote:
> Matt Garrish wrote:
> > "meltedown" <groups2 [at] reenie.org> wrote in message
> > news:Neqdf.19659$Ww6.10326 [at] fe05.news.easynews.com...
> >
[problem description snipped]
> > The following is hack that will do what
> > you want, but hopefully will give you an idea about why you need to do more
> > than write a simple regular expression. (watch for wrapping on the regex)
> >
> > my $string = <<STR;
> > php is no perl
> > <textarea>for reasons
> > that are too complicated
> > to explain here</textarea>
> > but you're free to google
> > them for yourself
> > STR
> >
> > my $done;
> > $string =~ s#(.*?)(<textarea[^>]*>.*?</textarea>)?#my $txtarea=$2;(my $text
> > = $1)=~s/\n//;$done .="$text$txtarea"#gise;
> > print $done;
> >
>
> Thanks, but isn't there supposed to be a semi colon after
> $string =~ s#(.*?)(<textarea[^>]*>.*?</textarea>)?#
>
No. The s#...# is the first part of the substitution operator, written
by Matt to use # as a delimiter: s#...#...#gise;
The entire statement is in two lines:
$string =~ s#...#...
....#gise;
> Is that a typo or is that supposed to be like that ?
> I'm just trying to make sure I understand it right.
Did it work when you tried it? Did you get a compile error when you
added a semicolon?
>
> > If you can't see why it's an abuse of the /e modifier then I wish you all
> > the luck in your programming future!
> >
> > Matt
I am leaving Matt's good advice in my response.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Perl » alt.perl » selective replacements