FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » finding newlines
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
finding newlines [message #183918] Mon, 25 November 2013 16:12 Go to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
I am trying to find the first line bread in a string
where string = "Hand Carried by Patient\n\nTo Whom It May
Concern:\n\nR...."

This code does not work: $pos = strpos($progressNote,"\n\n");
it returns pos = false

where do I err?

bill
Re: finding newlines [message #183919 is a reply to message #183918] Mon, 25 November 2013 16:32 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
bill wrote:

> I am trying to find the first line bread in a string
> where string = "Hand Carried by Patient\n\nTo Whom It May
> Concern:\n\nR...."
>
> This code does not work: $pos = strpos($progressNote,"\n\n");
> it returns pos = false
>
> where do I err?

Um, the code should work, if $progressNote does contain a *double*
newline character. Have you confirmed that $progressNote indeed
contains what you expect? You may try looking for a *single* newline
character:

$pos = strpos($progressNote, "\n");

--
Christoph M. Becker
Re: finding newlines [message #183920 is a reply to message #183918] Mon, 25 November 2013 16:34 Go to previous messageGo to next message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
On Mon, 25 Nov 2013 11:12:17 -0500, bill <william(at)TechServSys(dot)com>
wrote:

> I am trying to find the first line bread in a string
> where string = "Hand Carried by Patient\n\nTo Whom It May
> Concern:\n\nR...."
>
> This code does not work: $pos = strpos($progressNote,"\n\n");
> it returns pos = false
>
> where do I err?
>
> bill

Best to include your actual code so typos can be spotted. This works,
but I do not know how it differs from yours:

<?php
$progressNote="Hand Carried by Patient\n\nTo Whom It May
Concern:\n\n";
$pos = strpos($progressNote,"\n\n");
echo $pos;
exit;
// result is 23 which is correct
?>
Re: finding newlines [message #183921 is a reply to message #183918] Mon, 25 November 2013 20:11 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Mon, 25 Nov 2013 11:12:17 -0500, bill wrote:

> I am trying to find the first line bread in a string where string =
> "Hand Carried by Patient\n\nTo Whom It May Concern:\n\nR...."
>
> This code does not work: $pos = strpos($progressNote,"\n\n");
> it returns pos = false
>
> where do I err?

If you're trying to find lines in a multiline string, you could try
splitting the string on newline characters:

$lines=explode("\n",$progressNote);

Then you can work through $lines looking for relevant ones.

Note that you may have some empty strings in $lines, and also that you
may have \r\n where you expect \n.

A better mechanism may be:

$lines=explode("\n",preg_replace("/[\r\n]+/","\n",$progressNote));

Which will change all instances of any combination of one or more
newlines and / or carriage returns to a single newline, and then split
the string on the newline character.

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: finding newlines [message #183922 is a reply to message #183918] Mon, 25 November 2013 21:18 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
bill wrote:

> I am trying to find the first line bread in a string
> where string = "Hand Carried by Patient\n\nTo Whom It May
^^^^^^^^^^^^^^
For some reason I read “tomato” there at first ;-)

> Concern:\n\nR...."
>
> This code does not work: $pos = strpos($progressNote,"\n\n");
> it returns pos = false

No, it does not:

$ php -r 'var_dump(strpos("Hand Carried by Patient\n\nTo Whom It May",
"\n\n"));'
int(23)

> where do I err?

$progressNote !== "Hand Carried by Patient\n\n…"

PHP’s string functions are tricky; when in doubt, RTFM.


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(at)news(dot)demon(dot)co(dot)uk> (2004)
Re: finding newlines [message #183923 is a reply to message #183921] Mon, 25 November 2013 21:23 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Denis McMahon wrote:

> A better mechanism may be:
>
> $lines=explode("\n",preg_replace("/[\r\n]+/","\n",$progressNote));
>
> Which will change all instances of any combination of one or more
> newlines and / or carriage returns to a single newline, and then split
> the string on the newline character.

$lines = preg_split('/[\\r\\n]+/', $progressNote);

or (having empty lines as empty-string array elements)

$lines = preg_split('/\\r?\\n|\\r/', $progressNote);

See also: <http://php.net/file>


PointedEars
--
When all you know is jQuery, every problem looks $(olvable).
Re: finding newlines [message #183924 is a reply to message #183918] Mon, 25 November 2013 21:36 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 11/25/2013 11:12 AM, bill wrote:
> I am trying to find the first line bread in a string
> where string = "Hand Carried by Patient\n\nTo Whom It May
> Concern:\n\nR...."
>
> This code does not work: $pos = strpos($progressNote,"\n\n");
> it returns pos = false
>
> where do I err?
>
> bill

How did you get the string in $progressNote? Did you set it yourself,
or was it possibly sourced from a web page?

What do you get if you echo $progressNote?


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: finding newlines [message #183932 is a reply to message #183919] Tue, 26 November 2013 14:36 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 11/25/2013 11:32 AM, Christoph Michael Becker wrote:
> bill wrote:
>
>> I am trying to find the first line bread in a string
>> where string = "Hand Carried by Patient\n\nTo Whom It May
>> Concern:\n\nR...."
>>
>> This code does not work: $pos = strpos($progressNote,"\n\n");
>> it returns pos = false
>>
>> where do I err?
>
> Um, the code should work, if $progressNote does contain a *double*
> newline character. Have you confirmed that $progressNote indeed
> contains what you expect? You may try looking for a *single* newline
> character:
>
> $pos = strpos($progressNote, "\n");
>

the text above is the exact text I am searching.
The statement is:


$pos = stripos($progressNote,"\n\n");

echo basename(__FILE__) . ": " . __LINE__ . ":pos= $pos, first
string = >" . substr($progressNote, 0, $pos). "<<br />";
Re: finding newlines [message #183933 is a reply to message #183921] Tue, 26 November 2013 18:05 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Mon, 25 Nov 2013 20:11:40 +0000, Denis McMahon wrote:

> $lines=explode("\n",preg_replace("/[\r\n]+/","\n",$progressNote));

or of course:

$lines=preg_split("/[\r\n]+/",$progressNote);

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: finding newlines [message #183934 is a reply to message #183933] Tue, 26 November 2013 18:46 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Denis McMahon wrote:

> On Mon, 25 Nov 2013 20:11:40 +0000, Denis McMahon wrote:
>> $lines=explode("\n",preg_replace("/[\r\n]+/","\n",$progressNote));
>
> or of course:
>
> $lines=preg_split("/[\r\n]+/",$progressNote);

Now that I told you. However, “of course” *that* is questionable: within
double-quotes, “\r” and “\n” are expanded as *string* escape sequences,
before preg_split() sees them. When in doubt, use either single-quotes or
double-quotes with double-backslash.

I prefer to use double-backslash also with single-quotes in case someone
decides in the future to support more escape sequences. The resulting code
style also is more consistent because with trailing backslash you *must* use
double-backslash unless you want to escape the string delimiter: '\' is
likely to become a syntax error (unterminated string literal); '\\' is not.


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(at)news(dot)demon(dot)co(dot)uk> (2004)
Re: finding newlines [message #183936 is a reply to message #183924] Wed, 27 November 2013 10:11 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 11/25/2013 4:36 PM, Jerry Stuckle wrote:
> On 11/25/2013 11:12 AM, bill wrote:
>> I am trying to find the first line bread in a string
>> where string = "Hand Carried by Patient\n\nTo Whom It May
>> Concern:\n\nR...."
>>
>> This code does not work: $pos = strpos($progressNote,"\n\n");
>> it returns pos = false
>>
>> where do I err?
>>
>> bill
>
> How did you get the string in $progressNote? Did you set it
> yourself, or was it possibly sourced from a web page?
>
> What do you get if you echo $progressNote?
>
>
I create the progress note in another script that is POSTed to
this one.

looking at progress note tells me that is is as I said, actually,
I copied it from the display of the debug statement:

echo basename(__FILE__) . ": " . __LINE__ .
":progNote=>$progressNote<<br />";

As it should work, and does not, I will spend more time on it
tomorrow (when I have the time.

thanks

bill
Re: finding newlines [message #183940 is a reply to message #183936] Wed, 27 November 2013 15:56 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
bill wrote:

> I create the progress note in another script that is POSTed to
> this one.
>
> looking at progress note tells me that is is as I said, actually,
> I copied it from the display of the debug statement:
>
> echo basename(__FILE__) . ": " . __LINE__ .
> ":progNote=>$progressNote<<br />";
^
This is almost certainly an error. Also, you want to htmlspecialchars()
etc. the value of $progressNote.


PointedEars
--
When all you know is jQuery, every problem looks $(olvable).
Re: finding newlines [message #183941 is a reply to message #183936] Wed, 27 November 2013 16:47 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 11/27/2013 5:11 AM, bill wrote:
> On 11/25/2013 4:36 PM, Jerry Stuckle wrote:
>> On 11/25/2013 11:12 AM, bill wrote:
>>> I am trying to find the first line bread in a string
>>> where string = "Hand Carried by Patient\n\nTo Whom It May
>>> Concern:\n\nR...."
>>>
>>> This code does not work: $pos = strpos($progressNote,"\n\n");
>>> it returns pos = false
>>>
>>> where do I err?
>>>
>>> bill
>>
>> How did you get the string in $progressNote? Did you set it
>> yourself, or was it possibly sourced from a web page?
>>
>> What do you get if you echo $progressNote?
>>
>>
> I create the progress note in another script that is POSTed to this one.
>
> looking at progress note tells me that is is as I said, actually, I
> copied it from the display of the debug statement:
>
> echo basename(__FILE__) . ": " . __LINE__ .
> ":progNote=>$progressNote<<br />";
>
> As it should work, and does not, I will spend more time on it tomorrow
> (when I have the time.
>
> thanks
>
> bill

OK, in that case you have the characters '\' and 'n'. In PHP,
"\n" is a newline character. You need to either escape the '\' as in
"\\n\\n", or use single quotes around your string, i.e. '\n\n'.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: finding newlines [message #183946 is a reply to message #183941] Thu, 28 November 2013 16:09 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 11/27/2013 11:47 AM, Jerry Stuckle wrote:
> On 11/27/2013 5:11 AM, bill wrote:
>> On 11/25/2013 4:36 PM, Jerry Stuckle wrote:
>>> On 11/25/2013 11:12 AM, bill wrote:
>>>> I am trying to find the first line bread in a string
>>>> where string = "Hand Carried by Patient\n\nTo Whom It May
>>>> Concern:\n\nR...."
>>>>
>>>> This code does not work: $pos = strpos($progressNote,"\n\n");
>>>> it returns pos = false
>>>>
>>>> where do I err?
>>>>
>>>> bill
>>>
>>> How did you get the string in $progressNote? Did you set it
>>> yourself, or was it possibly sourced from a web page?
>>>
>>> What do you get if you echo $progressNote?
>>>
>>>
>> I create the progress note in another script that is POSTed to
>> this one.
>>
>> looking at progress note tells me that is is as I said,
>> actually, I
>> copied it from the display of the debug statement:
>>
>> echo basename(__FILE__) . ": " . __LINE__ .
>> ":progNote=>$progressNote<<br />";
>>
>> As it should work, and does not, I will spend more time on it
>> tomorrow
>> (when I have the time.
>>
>> thanks
>>
>> bill
>
> OK, in that case you have the characters '\' and 'n'. In PHP,
> "\n" is a newline character. You need to either escape the '\'
> as in "\\n\\n", or use single quotes around your string, i.e.
> '\n\n'.
>
Very good - that was it.
Thanks,

bill
Re: finding newlines [message #183948 is a reply to message #183940] Thu, 28 November 2013 16:42 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 11/27/2013 10:56 AM, Thomas 'PointedEars' Lahn wrote:
> bill wrote:
>
>> I create the progress note in another script that is POSTed to
>> this one.
>>
>> looking at progress note tells me that is is as I said, actually,
>> I copied it from the display of the debug statement:
>>
>> echo basename(__FILE__) . ": " . __LINE__ .
>> ":progNote=>$progressNote<<br />";
> ^
> This is almost certainly an error. Also, you want to htmlspecialchars()
> etc. the value of $progressNote.
>
>
> PointedEars
>

actually, not an error. I use the open and close bracket
characters to help me see the string and not miss spaces on
either end.

bill
Re: finding newlines [message #183951 is a reply to message #183948] Thu, 28 November 2013 19:20 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
bill wrote:

> On 11/27/2013 10:56 AM, Thomas 'PointedEars' Lahn wrote:
>> bill wrote:
>>> I create the progress note in another script that is POSTed to
>>> this one.
>>>
>>> looking at progress note tells me that is is as I said, actually,
>>> I copied it from the display of the debug statement:
>>>
>>> echo basename(__FILE__) . ": " . __LINE__ .
>>> ":progNote=>$progressNote<<br />";
>> ^
>> This is almost certainly an error. Also, you want to htmlspecialchars()
>> etc. the value of $progressNote.
>
> actually, not an error. I use the open and close bracket
> characters to help me see the string and not miss spaces on
> either end.

“<br />” is either XHTML or HTML5 syntax. In HTML, in XHTML and in other
XML applications, “<” and ”>” are special markup characters (Start-Tag Open
[STAGO] and Tag Close [TAGC]). You need to escape literal ”<” (in order to
prevent it from being parsed as STAGO, at least when a letter follows) and
you should escape literal ”>” (if only to avoid confusion). PHP's
htmlspecialchars() also does that for you.

<http://validator.w3.org/>


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(at)news(dot)demon(dot)co(dot)uk>
Re: finding newlines [message #183953 is a reply to message #183948] Fri, 29 November 2013 00:57 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Thu, 28 Nov 2013 11:42:50 -0500, bill wrote:

> On 11/27/2013 10:56 AM, Thomas 'PointedEars' Lahn wrote:
>> bill wrote:
>>
>>> I create the progress note in another script that is POSTed to this
>>> one.
>>>
>>> looking at progress note tells me that is is as I said, actually,
>>> I copied it from the display of the debug statement:
>>>
>>> echo basename(__FILE__) . ": " . __LINE__ .
>>> ":progNote=>$progressNote<<br />";
>> ^
>> This is almost certainly an error. Also, you want to
>> htmlspecialchars()
>> etc. the value of $progressNote.

> actually, not an error. I use the open and close bracket characters to
> help me see the string and not miss spaces on either end.

If you're outputting this in an xhtml document, that's an incredibly bad
idea. You should use &gt; and &lt; instead.

echo basename(__FILE__) . ": " . __LINE__ .
":progNote=&gt;$progressNote&lt;<br />";

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: finding newlines [message #183990 is a reply to message #183934] Sat, 30 November 2013 16:50 Go to previous messageGo to next message
somewhere is currently offline  somewhere
Messages: 2
Registered: November 2013
Karma: 0
Junior Member
Thomas 'PointedEars' Lahn wrote:

> Now that I told you. However, “of course” *that* is questionable:
> within double-quotes, “\r” and “\n” are expanded as *string* escape
> sequences,
> before preg_split() sees them. When in doubt, use either single-quotes
> or double-quotes with double-backslash.

Are you sure?
Re: finding newlines [message #183995 is a reply to message #183990] Sun, 01 December 2013 16:11 Go to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Wizard-Of-Oz wrote:
^^^^^^^^^^^^

> Thomas 'PointedEars' Lahn wrote:
>> Now that I told you. However, “of course” *that* is questionable:
>> within double-quotes, “\r” and “\n” are expanded as *string* escape
>> sequences, before preg_split() sees them. When in doubt, use either
>> single-quotes or double-quotes with double-backslash.
>
> Are you sure?

Who's asking?


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(at)news(dot)demon(dot)co(dot)uk>
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Lan Pc Login time
Next Topic: Check if $_GET contains something other than what's allowed
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 17:17:07 GMT 2024

Total time taken to generate the page: 0.02409 seconds